home *** CD-ROM | disk | FTP | other *** search
/ Hot Super Models / Hot Super Models.iso / unix / x11 / xv200.tar / xv-2.00 / xvgam.c < prev    next >
C/C++ Source or Header  |  1992-01-02  |  71KB  |  2,689 lines

  1. /* 
  2.  * xvgam.c
  3.  *
  4.  * callable functions:
  5.  *   <many>
  6.  */
  7.  
  8. /*
  9.  * Copyright 1989, 1990, 1991, 1992 by John Bradley and
  10.  *                       The University of Pennsylvania
  11.  *
  12.  * Permission to use, copy, and distribute for non-commercial purposes,
  13.  * is hereby granted without fee, providing that the above copyright
  14.  * notice appear in all copies and that both the copyright notice and this
  15.  * permission notice appear in supporting documentation. 
  16.  *
  17.  * The software may be modified for your own purposes, but modified versions
  18.  * may not be distributed.
  19.  *
  20.  * This software is provided "as is" without any expressed or implied warranty.
  21.  *
  22.  * The author may be contacted via:
  23.  *    US Mail:   John Bradley
  24.  *               GRASP Lab, Room 301C
  25.  *               3401 Walnut St.  
  26.  *               Philadelphia, PA  19104
  27.  *
  28.  *    Phone:     (215) 898-8813
  29.  *    EMail:     bradley@cis.upenn.edu       
  30.  */
  31.  
  32.  
  33. #include "xv.h"
  34. #include "bitmaps.h"
  35.  
  36. #define GAMW 664
  37. #define GAMH 518
  38.  
  39. #define GAMbutF 386
  40.  
  41. #define CMAPX  10
  42. #define CMAPY  17
  43. #define CMAPCW 12
  44. #define CMAPCH 10
  45. #define CMAPW  (CMAPCW * 16)
  46. #define CMAPH  (CMAPCH * 16)
  47.  
  48. #define MAXUNDO 32
  49.  
  50. #define BUTTH   23
  51. #define N_HMAP   6     /* # of Hue modification remappings */
  52.  
  53. #define N_HDBUTT 5
  54. #define HDB_ROTL  0
  55. #define HDB_ROTR  1
  56. #define HDB_EXPND 2
  57. #define HDB_SHRNK 3
  58. #define HDB_FLIP  4
  59.  
  60. #define N_HDBUTT2 4
  61. #define HDB_DESAT 2
  62. #define HDB_SAT   3
  63.  
  64.  
  65. #define HD_CLEAR  0x01   /* clears inside of hue dial */
  66. #define HD_FRAME  0x02
  67. #define HD_HANDS  0x04
  68. #define HD_DIR    0x08
  69. #define HD_VALS   0x10
  70. #define HD_TITLE  0x20
  71. #define HD_CLHNDS 0x40
  72. #define HD_BUTTS  0x80
  73. #define HD_ALL   (HD_FRAME | HD_HANDS | HD_DIR | HD_VALS | HD_TITLE | HD_BUTTS)
  74.  
  75. #define HD_RADIUS 30   /* radius of bounding circle of HDIALs */
  76.  
  77. #define DEG2RAD (3.14159 / 180.0)
  78. #define RAD2DEG (180.0 / 3.14159)
  79.  
  80.  
  81.  
  82. /* stuff for colormap Undo button */
  83. struct cmapstate { byte  r[256], g[256], b[256];
  84.            int cellgroup[256];
  85.            int curgroup;
  86.            int maxgroup;
  87.            int editColor;
  88.          } prevcmap, tmpcmap;
  89.  
  90. struct hmap {    int src_st;
  91.          int src_en;
  92.          int src_ccw;
  93.          int dst_st;
  94.          int dst_en;
  95.          int dst_ccw;
  96.            };
  97.  
  98. struct gamstate { struct hmap hmap[N_HMAP];
  99.           int hueRBnum;                          /* 1 - 6 */
  100.           int wht_stval, wht_satval, wht_enab;
  101.           int satval;
  102.           GRAF_STATE istate, rstate, gstate, bstate;
  103.         } undo[MAXUNDO], preset[4], defstate; 
  104. static int uptr, uhead, utail;
  105.  
  106. typedef struct huedial {
  107.          Window win;      /* window that dial exists in */
  108.          int    x,y;      /* coordinates of center of dial */
  109.                  int    range;    /* 0 = single value, 1 = range */
  110.          int    stval;    /* start of range (ONLY val ifnot range) */
  111.          int    enval;    /* end of range */
  112.          int    ccwise;   /* 1 if range goes ccwise, 0 if cwise */
  113.          char  *str;      /* title string */
  114.          u_long fg,bg;    /* colors */
  115.          int    satval;   /* saturation value on non-range dial */
  116.          BUTT   hdbutt[N_HDBUTT];
  117.          CBUTT  enabCB;
  118.            } HDIAL;
  119.  
  120.  
  121.  
  122. static BUTT   hueclrB;
  123. static CBUTT  enabCB, autoCB, resetCB;
  124. static HDIAL  srcHD, dstHD, whtHD;
  125. static DIAL   satDial, rhDial, gsDial, bvDial;
  126. static GRAF   intGraf, rGraf, gGraf, bGraf;
  127. static RBUTT *hueRB;
  128. static Window cmapF, hsvF, rgbF, modF, butF;
  129. static struct hmap hmap[N_HMAP];
  130. static int    hremap[360];
  131.  
  132.  
  133.  
  134. #ifdef __STDC__
  135. static void changedGam();
  136. static void drawGam(int,int,int,int);
  137. static void drawBut(int,int,int,int);
  138. static void drawArrow(int, int);
  139. static void drawCmap(void);
  140. static void clickCmap(int,int,int);
  141. static void clickGam(int,int);
  142. static void selectCell(int,int);
  143. static int  deladdCell(int, int);
  144. static void doCmd(int);
  145. static void SetHSVmode(void);
  146. static void applyGamma();
  147. static void saveGamState(void);
  148. static void gamUndo(void);
  149. static void gamRedo(void);
  150. static void save_gstate(struct gamstate *);
  151. static void restore_gstate(struct gamstate *);
  152. static void rndCols(void);
  153. static void saveCMap(struct cmapstate *);
  154. static void restoreCMap(struct cmapstate *);
  155. static void parseResources(void);
  156. static void makeResources(void);
  157.  
  158. static void HDCreate(HDIAL *, Window, int, int, int, int, int, int, char *,
  159.               u_long, u_long);
  160. static void HDRedraw(HDIAL *, int);
  161. static int  HDClick(HDIAL *, int, int);
  162. static int  HDTrack(HDIAL *, int, int);
  163. static int  hdg2xdg(int);
  164. static void pol2xy(int, int, double, int, int *, int *);
  165. static int  computeHDval(HDIAL *, int, int);
  166. static void initHmap(void);
  167. static void init1hmap(int);
  168. static void dials2hmap(void);
  169. static void hmap2dials(void);
  170. static void build_hremap(void);
  171.  
  172. #else
  173. static void changedGam(), drawGam(), drawBut(), drawArrow(), drawCmap();
  174. static void clickCmap(), clickGam(), selectCell();
  175. static void doCmd(), applyGamma();
  176. static int  deladdCell();
  177. static void SetHSVmode(), saveGamState(), gamUndo(), gamRedo();
  178. static void save_gstate(), restore_gstate();
  179. static void rndCols(), saveCMap(), restoreCMap(), parseResources();
  180. static void makeResources();
  181.  
  182. static void HDCreate(), HDRedraw(), pol2xy();
  183. static int  HDClick(), HDTrack(), hdg2xdg(), computeHDval();
  184. static void initHmap(), init1hmap(), dials2hmap(), hmap2dials();
  185. static void build_hremap();
  186. #endif
  187.  
  188.  
  189.  
  190.  
  191.  
  192. /***************************************************/
  193. void CreateGam(geom)
  194. char *geom;
  195. {
  196.   XClassHint classh;
  197.   CARD32     data[2];
  198.   Atom       prop;
  199.  
  200.   gamW = CreateWindow("xv color editor", geom, GAMW,GAMH, infofg,infobg);
  201.   if (!gamW) FatalError("can't create cedit window!");
  202.  
  203.   classh.res_name = "xv";
  204.   classh.res_class = "XVcedit";
  205.   XSetClassHint(theDisp, gamW, &classh);
  206.  
  207.   data[0] = (CARD32)XInternAtom(theDisp, "WM_DELETE_WINDOW", FALSE);
  208.   data[1] = (CARD32)time((long *)0);
  209.   prop = XInternAtom(theDisp, "WM_PROTOCOLS", FALSE),
  210.  
  211.   XChangeProperty(theDisp, gamW, prop, prop,
  212.           32, PropModeReplace, (unsigned char *) data, 2);
  213.  
  214.   cmapF = XCreateSimpleWindow(theDisp,gamW, 10,   8,212,339, 1,infofg,infobg);
  215.   butF  = XCreateSimpleWindow(theDisp,gamW, 10, 353,212, 96, 1,infofg,infobg);
  216.   modF  = XCreateSimpleWindow(theDisp,gamW, 10, 455,212, 53, 1,infofg,infobg);
  217.   hsvF  = XCreateSimpleWindow(theDisp,gamW, 242,  8,205,500, 1,infofg,infobg);
  218.   rgbF  = XCreateSimpleWindow(theDisp,gamW, 467,  8,185,500, 1,infofg,infobg);
  219.  
  220.   if (!cmapF || !butF || !modF || !hsvF || !rgbF) 
  221.     FatalError("couldn't create frame windows");
  222.  
  223.   XSelectInput(theDisp, gamW,  ExposureMask);
  224.   XSelectInput(theDisp, cmapF, ExposureMask | ButtonPressMask);
  225.   XSelectInput(theDisp, butF,  ExposureMask | ButtonPressMask);
  226.   XSelectInput(theDisp, modF,  ExposureMask | ButtonPressMask);
  227.   XSelectInput(theDisp, hsvF,  ExposureMask | ButtonPressMask);
  228.   XSelectInput(theDisp, rgbF,  ExposureMask);
  229.  
  230.   if (ctrlColor) XSetWindowBackground(theDisp, gamW, locol);
  231.             else XSetWindowBackgroundPixmap(theDisp, gamW, grayTile);
  232.  
  233.   /********** COLORMAP editing doo-wahs ***********/
  234.  
  235.  
  236.   BTCreate(&gbut[G_BCOLUNDO], cmapF, 5, 182, 66, BUTTH, 
  237.        "ColUndo", infofg, infobg);
  238.   BTCreate(&gbut[G_BCOLREV], cmapF,  5 + 66 + 1, 182, 67, BUTTH, 
  239.        "Revert", infofg, infobg);
  240.   BTCreate(&gbut[G_BHSVRGB], cmapF,  5+66+67+2,  182, 66, BUTTH, 
  241.        "RGB/HSV", infofg, infobg);
  242.  
  243.   BTCreate(&gbut[G_BMONO], cmapF,    5, 206, 66, BUTTH, 
  244.        "Grey", infofg, infobg);
  245.   BTCreate(&gbut[G_BRV],   cmapF,    5 + 66 + 1, 206, 67, BUTTH, 
  246.        "RevVid", infofg, infobg);
  247.   BTCreate(&gbut[G_BRNDCOL], cmapF,  5 + 66 + 67 + 2, 206, 66, BUTTH, 
  248.        "Random", infofg, infobg);
  249.  
  250.   DCreate(&rhDial, cmapF, 5, 232, 66, 100,   0,360,180, 5, 
  251.       infofg, infobg, "Hue", NULL);
  252.   DCreate(&gsDial, cmapF, 72, 232, 66, 100,  0,360,180, 5, 
  253.       infofg, infobg, "Sat.", NULL);
  254.   DCreate(&bvDial, cmapF, 139, 232, 66, 100,   0,360,180, 5, 
  255.       infofg, infobg, "Value", NULL);
  256.  
  257.  
  258.   /*********** CONTROL BUTTONS ***********/
  259.  
  260. /* positioning constants for buttons.  (arranged as 4x4 grid...) */
  261. #define BXSPACE 53
  262. #define BYSPACE (BUTTH+1)
  263.  
  264. #define BX0 0
  265. #define BX1 (BX0 + BXSPACE)
  266. #define BX2 (BX0 + BXSPACE*2)
  267. #define BX3 (BX0 + BXSPACE*3)
  268.  
  269. #define BY0 0
  270. #define BY1 (BY0 + BYSPACE)
  271. #define BY2 (BY0 + BYSPACE*2)
  272. #define BY3 (BY0 + BYSPACE*3)
  273.  
  274.   BTCreate(&gbut[G_BAPPLY],  butF, BX0,BY0, 52,BUTTH,"Apply", infofg,infobg);
  275.   BTCreate(&gbut[G_BNOGAM],  butF, BX0,BY1, 52,BUTTH,"NoMod", infofg,infobg);
  276.   BTCreate(&gbut[G_BMAXCONT],butF, BX0,BY2, 52,BUTTH,"MaxCn", infofg,infobg);
  277.   BTCreate(&gbut[G_BRESET],  butF, BX0,BY3, 52,BUTTH,"Reset", infofg,infobg);
  278.  
  279.   BTCreate(&gbut[G_BUP_BR],butF, BX1,BY0, 52,BUTTH,"Brite",infofg,infobg);
  280.   BTCreate(&gbut[G_BDN_BR],butF, BX1,BY1, 52,BUTTH,"Dim",  infofg,infobg);
  281.   BTCreate(&gbut[G_BUP_CN],butF, BX1,BY2, 52,BUTTH,"Sharp",infofg,infobg);
  282.   BTCreate(&gbut[G_BDN_CN],butF, BX1,BY3, 52,BUTTH,"Dull", infofg,infobg);
  283.  
  284.   BTCreate(&gbut[G_B1],    butF, BX2,   BY0, 25,BUTTH,"1",    infofg,infobg);
  285.   BTCreate(&gbut[G_B2],    butF, BX2+26,BY0, 26,BUTTH,"2",    infofg,infobg);
  286.   BTCreate(&gbut[G_B3],    butF, BX2,   BY1, 25,BUTTH,"3",    infofg,infobg);
  287.   BTCreate(&gbut[G_B4],    butF, BX2+26,BY1, 26,BUTTH,"4",    infofg,infobg);
  288.   BTCreate(&gbut[G_BSET],  butF, BX2,   BY2, 52,BUTTH,"Set",  infofg,infobg);
  289.   BTCreate(&gbut[G_BGETRES],butF,BX2,   BY3, 105,BUTTH,"Cut Resources", 
  290.        infofg,infobg);
  291.  
  292.   BTCreate(&gbut[G_BUNDO], butF, BX3, BY0, 52,BUTTH,"Undo", infofg,infobg);
  293.   BTCreate(&gbut[G_BREDO], butF, BX3, BY1, 52,BUTTH,"Redo", infofg,infobg);
  294.   BTCreate(&gbut[G_BCLOSE],butF, BX3, BY2, 52,BUTTH,"Close", infofg,infobg);
  295.  
  296.  
  297.   gbut[G_BSET].toggle = 1;
  298.   gbut[G_BUNDO].active = 0;
  299.   gbut[G_BREDO].active = 0;
  300.  
  301.   CBCreate(&enabCB, modF,2,2,     "Display with HSV/RGB mods.",infofg,infobg);
  302.   CBCreate(&autoCB, modF,2,2+17,  "Auto-apply HSV/RGB mods.",  infofg,infobg);
  303.   CBCreate(&resetCB,modF,2,2+17*2,"Auto-reset on new image.",  infofg,infobg);
  304.   enabCB.val = autoCB.val = resetCB.val = 1;
  305.  
  306.  
  307.   /************ HSV editing doo-wahs **************/
  308.  
  309.  
  310.   HDCreate(&srcHD, hsvF,  51, 65, 1, 0, 30, 0, "From", infofg, infobg);
  311.   HDCreate(&dstHD, hsvF, 155, 65, 1, 0, 30, 0, "To",   infofg, infobg);
  312.  
  313.   HDCreate(&whtHD, hsvF,  50,243, 0, 0,  0, 0, "White",infofg, infobg);
  314.  
  315.   DCreate(&satDial, hsvF, 100, 199, 100, 121, -100, 100, 0, 5, 
  316.        infofg, infobg, "Saturation", "%");
  317.  
  318.   hueRB = RBCreate(NULL, hsvF,  7, 153, "1", infofg, infobg);
  319.   RBCreate        (hueRB,hsvF, 47, 153, "2", infofg, infobg);
  320.   RBCreate        (hueRB,hsvF, 87, 153, "3", infofg, infobg);
  321.   RBCreate        (hueRB,hsvF,  7, 170, "4", infofg, infobg);
  322.   RBCreate        (hueRB,hsvF, 47, 170, "5", infofg, infobg);
  323.   RBCreate        (hueRB,hsvF, 87, 170, "6", infofg, infobg);
  324.  
  325.   BTCreate(&hueclrB, hsvF, 127, 158, 70, BUTTH, "Reset", infofg, infobg);
  326.  
  327.   initHmap();
  328.   hmap2dials();
  329.   build_hremap();
  330.  
  331.   InitGraf(&intGraf);
  332.   CreateGraf(&intGraf, hsvF, 20, 339, infofg, infobg, "Intensity");
  333.  
  334.  
  335.   /********* RGB color correction doo-wahs ***********/
  336.  
  337.  
  338.   InitGraf(&rGraf);
  339.   CreateGraf(&rGraf, rgbF, 10, 20, infofg, infobg, "Red");
  340.  
  341.   InitGraf(&gGraf);
  342.   CreateGraf(&gGraf, rgbF, 10, 179, infofg, infobg, "Green");
  343.   
  344.   InitGraf(&bGraf);
  345.   CreateGraf(&bGraf, rgbF, 10, 338, infofg, infobg, "Blue");
  346.   
  347.   SetHSVmode();
  348.  
  349.   save_gstate(&defstate);
  350.   save_gstate(&preset[0]);
  351.   save_gstate(&preset[1]);
  352.   save_gstate(&preset[2]);
  353.   save_gstate(&preset[3]);
  354.   
  355.   parseResources();
  356.   restore_gstate(&defstate);   /* defstate may have changed due to resources */
  357.  
  358.   uptr = utail = uhead = 0;
  359.   save_gstate(&undo[0]);
  360.  
  361.   XMapSubwindows(theDisp, cmapF);
  362.   XMapSubwindows(theDisp, hsvF);
  363.   XMapSubwindows(theDisp, rgbF);
  364.   XMapSubwindows(theDisp, gamW);
  365. }
  366.   
  367.  
  368. /***************************************************/
  369. int GamCheckEvent(xev)
  370. XEvent *xev;
  371. {
  372.   /* check event to see if it's for one of our subwindows.  If it is,
  373.      deal accordingly, and return '1'.  Otherwise, return '0' */
  374.  
  375.   int rv;
  376.  
  377.   rv = 1;
  378.   
  379.   if (xev->type == Expose) {
  380.     int x,y,w,h;
  381.     XExposeEvent *e = (XExposeEvent *) xev;
  382.     x = e->x;  y = e->y;  w = e->width;  h = e->height;
  383.  
  384.     /* throw away excess redraws for 'dumb' windows */
  385.     if (e->count > 0 && 
  386.     (e->window == satDial.win || e->window == rhDial.win ||
  387.      e->window == gsDial.win  || e->window == bvDial.win ||
  388.      e->window == cmapF       || e->window == modF       ||
  389.      e->window == intGraf.win || e->window == rGraf.win  ||
  390.      e->window == gGraf.win   || e->window == bGraf.win  ||
  391.      e->window == rgbF)) {}
  392.  
  393.     else if (e->window == gamW)        drawGam(x, y, w, h);
  394.     else if (e->window == butF)        drawBut(x, y, w, h);
  395.     else if (e->window == satDial.win) DRedraw(&satDial);
  396.     else if (e->window == rhDial.win)  DRedraw(&rhDial);
  397.     else if (e->window == gsDial.win)  DRedraw(&gsDial);
  398.     else if (e->window == bvDial.win)  DRedraw(&bvDial);
  399.  
  400.     else if (e->window == intGraf.win) RedrawGraf(&intGraf,x,y,w,h);
  401.     else if (e->window == rGraf.win)   RedrawGraf(&rGraf,  x,y,w,h);
  402.     else if (e->window == gGraf.win)   RedrawGraf(&gGraf,  x,y,w,h);
  403.     else if (e->window == bGraf.win)   RedrawGraf(&bGraf,  x,y,w,h);
  404.  
  405.     else if (e->window == cmapF) drawCmap();
  406.  
  407.     else if (e->window == modF) {
  408.       CBRedraw(&enabCB);
  409.       CBRedraw(&autoCB);
  410.       CBRedraw(&resetCB);
  411.     }
  412.  
  413.     else if (e->window == hsvF) {
  414.       XRectangle xr;
  415.       xr.x = x;  xr.y = y;  xr.width = e->width;  xr.height = e->height;
  416.       XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
  417.  
  418.       XSetForeground(theDisp, theGC, infofg);
  419.       ULineString(hsvF, "HSV Modification", 2, 1+ASCENT);
  420.  
  421.       HDRedraw(&srcHD, HD_ALL);
  422.       HDRedraw(&dstHD, HD_ALL);
  423.       HDRedraw(&whtHD, HD_ALL);
  424.       RBRedraw(hueRB, -1);
  425.       BTRedraw(&hueclrB);
  426.  
  427.       XSetClipMask(theDisp, theGC, None);
  428.     }
  429.  
  430.     else if (e->window == rgbF) {
  431.       XSetForeground(theDisp, theGC, infofg);
  432.       ULineString(rgbF, "RGB Modification", 2, 1+ASCENT);
  433.     }
  434.  
  435.     else rv = 0;
  436.   }
  437.  
  438.   else if (xev->type == ButtonPress) {
  439.     XButtonEvent *e = (XButtonEvent *) xev;
  440.     int i,x,y;
  441.     x = e->x;  y = e->y;
  442.  
  443.     if (e->button == Button1) {
  444.       if      (e->window == butF)  clickGam(x,y);
  445.       else if (e->window == cmapF) clickCmap(x,y,1);
  446.  
  447.       else if (e->window == modF) {
  448.     if (CBClick(&enabCB,x,y)) {
  449.       if (CBTrack(&enabCB)) applyGamma();  /* enabCB changed, regen */
  450.     }
  451.  
  452.     else if (CBClick(&autoCB,x,y)) {
  453.       CBTrack(&autoCB);
  454.       if (autoCB.val) applyGamma();  /* auto-apply turned on, apply now */
  455.     }
  456.  
  457.     else if (CBClick(&resetCB,x,y)) CBTrack(&resetCB);
  458.       }
  459.  
  460.  
  461.       else if (e->window == hsvF) {
  462.     if (HDClick(&srcHD, x,y) || HDClick(&dstHD, x,y)) { 
  463.       dials2hmap();
  464.       build_hremap();
  465.       changedGam();
  466.     }
  467.     else if (HDClick(&whtHD, x,y)) changedGam();
  468.  
  469.     else if (PTINRECT(x,y,hueclrB.x, hueclrB.y, hueclrB.w, hueclrB.h)) {
  470.       if (BTTrack(&hueclrB)) {   /* RESET */
  471.         dstHD.stval  = srcHD.stval;
  472.         dstHD.enval  = srcHD.enval;
  473.         dstHD.ccwise = srcHD.ccwise;
  474.         HDRedraw(&dstHD, HD_ALL | HD_CLEAR);
  475.         dials2hmap();
  476.         build_hremap();
  477.         changedGam();
  478.       }
  479.     }
  480.  
  481.     else if ((i=RBClick(hueRB,x,y)) >= 0) {
  482.       dials2hmap();
  483.       if (RBTrack(hueRB, i)) hmap2dials();
  484.     }
  485.       }
  486.  
  487.       else if (e->window == intGraf.win) {
  488.     GRAF_STATE gs;
  489.     if (ClickGraf(&intGraf, e->subwindow, x,y)) {
  490.       GetGrafState(&intGraf, &gs);
  491.       changedGam();
  492.     }
  493.       }
  494.  
  495.       else if (e->window == rGraf.win) {
  496.     if (ClickGraf(&rGraf, e->subwindow, x,y)) changedGam();
  497.       }
  498.  
  499.       else if (e->window == gGraf.win) {
  500.     if (ClickGraf(&gGraf, e->subwindow, x,y)) changedGam();
  501.       }
  502.  
  503.       else if (e->window == bGraf.win) {
  504.     if (ClickGraf(&bGraf, e->subwindow, x,y)) changedGam();
  505.       }
  506.  
  507.  
  508.       else if (e->window == satDial.win) {
  509.     if (DTrack(&satDial, x, y)) changedGam();
  510.       }
  511.  
  512.       else if (e->window == rhDial.win ||
  513.            e->window == gsDial.win ||
  514.            e->window == bvDial.win) {
  515.  
  516.     if ((e->window == rhDial.win && DTrack(&rhDial, x,y)) || 
  517.         (e->window == gsDial.win && DTrack(&gsDial, x,y)) ||
  518.         (e->window == bvDial.win && DTrack(&bvDial, x,y))) {
  519.       saveCMap(&prevcmap);
  520.       BTSetActive(&gbut[G_BCOLUNDO],1);
  521.       ApplyEditColor(0);
  522.     }
  523.       }
  524.  
  525.       else rv = 0;
  526.     }
  527.  
  528.     else if (e->button == Button3) {
  529.       if (e->window == cmapF) clickCmap(x, y, 3);
  530.       else rv = 0;
  531.     }
  532.     else rv = 0;
  533.   }
  534.  
  535.  
  536.   else if (xev->type == KeyPress) {
  537.     XKeyEvent *e = (XKeyEvent *) xev;
  538.     char buf[128];  KeySym ks;  XComposeStatus status;  
  539.     int stlen;
  540.     
  541.     stlen = XLookupString(e,buf,128,&ks,&status);
  542.     buf[stlen] = '\0';
  543.  
  544.     if (!stlen) return 0;
  545.  
  546.     else if (e->window == intGraf.win) rv = GrafKey(&intGraf,buf);
  547.     else if (e->window == rGraf.win  ) rv = GrafKey(&rGraf,buf);
  548.     else if (e->window == gGraf.win  ) rv = GrafKey(&gGraf,buf);
  549.     else if (e->window == bGraf.win  ) rv = GrafKey(&bGraf,buf);
  550.     else rv = 0;
  551.  
  552.     if (rv>1) changedGam();   /* hit 'enter' in one of Graf's */
  553.   }
  554.  
  555.   else rv = 0;
  556.  
  557.   return rv;
  558. }
  559.  
  560.  
  561.  
  562. /***************************************************/
  563. static void changedGam()
  564. {
  565.   /* called whenever an HSV/RGB gamma ctrl has changed
  566.      applies change to image if autoCB.val is set */
  567.  
  568.   saveGamState();
  569.   if (autoCB.val) applyGamma();
  570. }
  571.  
  572.  
  573. /***************************************************/
  574. void GamBox(vis)
  575. int vis;
  576. {
  577.   if (vis) XMapRaised(theDisp, gamW);
  578.   else     XUnmapWindow(theDisp, gamW);
  579.  
  580.   gamUp = vis;
  581. }
  582.  
  583.  
  584. /***************************************************/
  585. static void drawGam(x,y,w,h)
  586. int x,y,w,h;
  587. {
  588.   XRectangle xr;
  589.  
  590.   xr.x = x;  xr.y = y;  xr.width = w;  xr.height = h;
  591.   XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
  592.  
  593.   drawArrow(232,178);
  594.   drawArrow(457,178);
  595.  
  596.   XSetClipMask(theDisp, theGC, None);
  597. }
  598.  
  599.  
  600. /***************************************************/
  601. static void drawBut(x,y,w,h)
  602. int x,y,w,h;
  603. {
  604.   int i;
  605.   XRectangle xr;
  606.  
  607.   xr.x = x;  xr.y = y;  xr.width = w;  xr.height = h;
  608.   XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
  609.  
  610.   for (i=0; i<G_NBUTTS; i++) {
  611.     if (gbut[i].win == butF) BTRedraw(&gbut[i]);
  612.   }
  613.  
  614.   XSetClipMask(theDisp, theGC, None);
  615. }
  616.  
  617.  
  618. /***************************************************/
  619. static void drawArrow(x,y)
  620. int x,y;
  621. {
  622.   XPoint pts[8];
  623.   
  624.   pts[0].x = x+10;     pts[0].y = y;
  625.   pts[1].x = x-4;      pts[1].y = y-100;
  626.   pts[2].x = x-4;      pts[2].y = y-40;
  627.   pts[3].x = x-10;     pts[3].y = y-40;
  628.   pts[4].x = x-10;     pts[4].y = y+40;
  629.   pts[5].x = x-4;      pts[5].y = y+40;
  630.   pts[6].x = x-4;      pts[6].y = y+100;
  631.   pts[7].x = pts[0].x; pts[7].y = pts[0].y;
  632.  
  633.   XSetForeground(theDisp, theGC, infobg);
  634.   XFillPolygon(theDisp, gamW, theGC, pts, 8, Convex, CoordModeOrigin);
  635.   XSetForeground(theDisp, theGC, infofg);
  636.   XDrawLines(theDisp, gamW, theGC, pts, 8, CoordModeOrigin);
  637. }
  638.  
  639.  
  640. /***************************************************/
  641. static void drawCmap()
  642. {
  643.   int i;
  644.  
  645.   XSetForeground(theDisp, theGC, infofg);
  646.   ULineString(cmapF, "Colormap Editing", 2, 1+ASCENT);
  647.  
  648.   for (i=0; i<G_NBUTTS; i++) {
  649.     if (gbut[i].win == cmapF) BTRedraw(&gbut[i]);
  650.   }
  651.  
  652.   RedrawCMap();
  653. }
  654.  
  655.  
  656.  
  657. /***************************************************/
  658. void NewCMap()
  659. {
  660.   /* called when we've loaded a new picture */
  661.  
  662.   int i;
  663.  
  664.   XClearArea(theDisp, cmapF, CMAPX, CMAPY, CMAPW+1, CMAPH+1, False);
  665.   for (i=0; i<256; i++) cellgroup[i] = 0;
  666.   curgroup = maxgroup = 0;
  667.  
  668.   BTSetActive(&gbut[G_BCOLUNDO],0);
  669.  
  670.   if (resetCB.val) {            /* auto-reset gamma controls */
  671.     i = autoCB.val;
  672.     if (i) autoCB.val = 0;      /* must NOT apply changes! */
  673.     restore_gstate(&defstate);
  674.     autoCB.val = i;
  675.   }
  676. }
  677.  
  678.  
  679. /***************************************************/
  680. void RedrawCMap()
  681. {
  682.   int i;
  683.  
  684.   XSetLineAttributes(theDisp, theGC, 0, LineSolid, CapButt, JoinMiter);
  685.   XSetForeground(theDisp, theGC, infofg);
  686.  
  687.   for (i=0; i<numcols; i++) {
  688.     int x,y;
  689.     x = CMAPX + (i%16)*CMAPCW;
  690.     y = CMAPY + (i/16)*CMAPCH;
  691.     XDrawRectangle(theDisp, cmapF, theGC, x, y, CMAPCW,CMAPCH);
  692.   }
  693.  
  694.   for (i=0; i<numcols; i++) {
  695.     int x,y;
  696.     x = CMAPX + (i%16)*CMAPCW;
  697.     y = CMAPY + (i/16)*CMAPCH;
  698.  
  699.     XSetForeground(theDisp, theGC, cols[i]);
  700.     XFillRectangle(theDisp, cmapF, theGC, x+1, y+1, CMAPCW-1,CMAPCH-1);
  701.  
  702.     if (i == editColor || (curgroup && (cellgroup[i]==curgroup))) {
  703.       XSetForeground(theDisp, theGC, infobg);
  704.       XDrawRectangle(theDisp, cmapF, theGC, x+1, y+1, CMAPCW-2,CMAPCH-2);
  705.       XSetForeground(theDisp, theGC, infofg);
  706.       XDrawRectangle(theDisp, cmapF, theGC, x+2, y+2, CMAPCW-4,CMAPCH-4);
  707.     }
  708.   }
  709. }
  710.  
  711.  
  712. /***************************************************/
  713. static void selectCell(cellno, sel)
  714. int cellno, sel;
  715. {
  716.   int x,y;
  717.  
  718.   if (cellno >= numcols) return;
  719.  
  720.   x = CMAPX + (cellno%16)*CMAPCW;
  721.   y = CMAPY + (cellno/16)*CMAPCH;
  722.  
  723.   if (!sel) {   /* unhighlight a cell */
  724.     XSetForeground(theDisp, theGC, cols[cellno]);
  725.     XFillRectangle(theDisp, cmapF, theGC, x+1, y+1, CMAPCW-1,CMAPCH-1);
  726.   }
  727.   else {  /* highlight a cell */
  728.     XSetForeground(theDisp, theGC, infobg);
  729.     XDrawRectangle(theDisp, cmapF, theGC, x+1, y+1, CMAPCW-2,CMAPCH-2);
  730.     XSetForeground(theDisp, theGC, infofg);
  731.     XDrawRectangle(theDisp, cmapF, theGC, x+2, y+2, CMAPCW-4,CMAPCH-4);
  732.   }
  733. }
  734.  
  735.  
  736. /***************************************************/
  737. static void clickGam(x,y)
  738. int x,y;
  739. {
  740.   int i;
  741.   BUTT *bp;
  742.  
  743.   for (i=0; i<G_NBUTTS; i++) {
  744.     bp = &gbut[i];
  745.     if (bp->win == butF && PTINRECT(x, y, bp->x, bp->y, bp->w, bp->h)) break;
  746.   }
  747.  
  748.   /* if 'Set' is lit, and we didn't click 'set' or '1'..'4', turn it off */
  749.   if (i!=G_BSET && i!=G_B1 && i!=G_B2 && i!=G_B3 && i!=G_B4 
  750.       && gbut[G_BSET].lit) {
  751.     gbut[G_BSET].lit = 0;  
  752.     BTRedraw(&gbut[G_BSET]);
  753.   }
  754.   
  755.  
  756.   if (i<G_NBUTTS) {  /* found one */
  757.     if (BTTrack(bp)) doCmd(i);
  758.   }
  759. }
  760.  
  761.  
  762. /***************************************************/
  763. static void clickCmap(x,y,but)
  764. int x,y,but;
  765. {
  766.   int i, recolor;
  767.   BUTT *bp;
  768.  
  769.   if (but==1) {   /* if left click, check the cmap controls */
  770.     for (i=0; i<G_NBUTTS; i++) {
  771.       bp = &gbut[i];
  772.       if (bp->win == cmapF && PTINRECT(x,y,bp->x, bp->y, bp->w, bp->h)) break;
  773.     }
  774.  
  775.     if (i<G_NBUTTS) {  /* found one */
  776.       if (BTTrack(bp)) doCmd(i);
  777.       return;
  778.     }
  779.   }
  780.  
  781.  
  782.   /* see if we're anywhere in the colormap area */
  783.  
  784.   if (PTINRECT(x,y,CMAPX,CMAPY,CMAPW,CMAPH)) {
  785.     if (but==1) {           /* select different cell/group for editing */
  786.       /* compute colorcell # */
  787.       i = ((x-CMAPX)/CMAPCW) + ((y-CMAPY)/CMAPCH)*16;
  788.       if (i<numcols) {    /* clicked in colormap.  track til mouseup */
  789.     if (i!=editColor) ChangeEC(i);
  790.  
  791.     while (1) {
  792.       Window       rW,cW;
  793.       int          rx,ry,x,y;
  794.       unsigned int mask;
  795.  
  796.       if (XQueryPointer(theDisp,cmapF,&rW,&cW,&rx,&ry,&x,&y,&mask)) {
  797.         if (!(mask & Button1Mask)) break;    /* button released */
  798.  
  799.         RANGE(x, CMAPX, CMAPX+CMAPW-1);
  800.         RANGE(y, CMAPY, CMAPY+CMAPH-1);
  801.  
  802.         i = ((x-CMAPX)/CMAPCW) + ((y-CMAPY)/CMAPCH)*16;
  803.         if (i<numcols && i != editColor) ChangeEC(i);
  804.       }
  805.     } /* while */
  806.       } /* if i<numcols */
  807.     } /* if but==1 */
  808.  
  809.  
  810.     if (but==3) { /* add/delete cell(s) from current group */
  811.       int lastcell,j,resetdel,curcell;
  812.  
  813.       /* better save the current cmap state, as it might change */
  814.       saveCMap(&tmpcmap);
  815.  
  816.       recolor = resetdel = 0;
  817.       /* compute colorcell # clicked in */
  818.       curcell = ((x-CMAPX)/CMAPCW) + ((y-CMAPY)/CMAPCH)*16;
  819.       if (curcell<numcols) {    /* clicked in colormap.  track til mouseup */
  820.     if (deladdCell(curcell, 1)) recolor=1;
  821.  
  822.     lastcell = curcell;
  823.  
  824.     j = XGrabPointer(theDisp, cmapF, False, 0, GrabModeAsync, 
  825.              GrabModeAsync, None, None, CurrentTime);
  826.     while (1) {
  827.       Window       rW,cW;
  828.       int          rx,ry,x,y;
  829.       unsigned int mask;
  830.  
  831.       if (XQueryPointer(theDisp,cmapF,&rW,&cW,&rx,&ry,&x,&y,&mask)) {
  832.         /* if button3 and shift released */
  833.         if (!(mask & (Button3Mask | ShiftMask))) break;  
  834.  
  835.         /* if user lets go of B3, reset addonly/delonly flag & lastcell */
  836.         if (!(mask & Button3Mask) && (mask & ShiftMask)) {
  837.           resetdel = 1;
  838.           lastcell = -1;
  839.         }
  840.  
  841.         if (mask & Button3Mask) {  /* select while b3 down */
  842.           RANGE(x, CMAPX, CMAPX+CMAPW-1);
  843.           RANGE(y, CMAPY, CMAPY+CMAPH-1);
  844.  
  845.           curcell = ((x-CMAPX)/CMAPCW) + ((y-CMAPY)/CMAPCH)*16;
  846.           if (curcell<numcols && curcell != lastcell) {
  847.         lastcell = curcell;
  848.         if (deladdCell(curcell, resetdel)) recolor=1;
  849.         resetdel = 0;
  850.           }
  851.         }
  852.       }
  853.     } /* while */
  854.  
  855.     XUngrabPointer(theDisp, CurrentTime);
  856.  
  857.     /* restore all cells that aren't in curgroup, unless curgroup=0,
  858.        in which case restore everything...  nasty */
  859.     for (i=0; i<numcols; i++) {
  860.       if (!curgroup || cellgroup[i]!=curgroup) {
  861.         rcmap[i] = tmpcmap.r[i];
  862.         gcmap[i] = tmpcmap.g[i];
  863.         bcmap[i] = tmpcmap.b[i];
  864.       }
  865.     }
  866.  
  867.     if (recolor) {
  868.       /* colors changed.  save to color undo area */
  869.       bcopy(&tmpcmap, &prevcmap, sizeof(struct cmapstate));
  870.       BTSetActive(&gbut[G_BCOLUNDO],1);
  871.       applyGamma();   /* have to regen entire image when groupings chg */
  872.     }
  873.  
  874.       } /* if i<numcols */
  875.     } /* if but==3 */
  876.  
  877.   } /* if PTINRECT */
  878. }
  879.  
  880.  
  881.  
  882.  
  883. /***************************************************/
  884. static int deladdCell(cnum, first)
  885. int cnum, first;
  886. {
  887.   int i,j,rv;
  888.   static int mode;
  889.  
  890. #define ADDORDEL 0
  891. #define DELONLY  1
  892. #define ADDONLY  2
  893.  
  894.   /* if 'first', we can add/delete cells as appropriate.  otherwise,
  895.      the (static) value of 'mode' determines whether we can
  896.      delete or add cells to groups.  The practical upshot is that it should
  897.      behave like the 'fat-bits pencil' in MacPaint */
  898.  
  899.   /* cases:  curgroup>0, clicked on something in same group
  900.                          remove target from group
  901.          curgroup>0, clicked on something in different group
  902.                      merge groups.  (target group gets 
  903.              set equal to current values)
  904.              curgroup>0, clicked on something in no group
  905.                      add target to curgroup
  906.              curgroup=0, clicked on something in a group
  907.                      add editColor to target group, 
  908.              set curgroup = target group
  909.              target group gets current values
  910.          curgroup=0, clicked on something in no group
  911.                      create a new group, add both cells to it
  912.    */
  913.  
  914.  
  915.   rv = 0;
  916.   if (first) mode = ADDORDEL;
  917.  
  918.   if (curgroup) {
  919.     if ((mode!=ADDONLY) && cellgroup[cnum] == curgroup) {
  920.       /* remove target from curgroup.  If it's the last one, delete group */
  921.  
  922.       for (i=0,j=0; i<numcols; i++) {     /* count #cells in this group */
  923.     if (cellgroup[i] == curgroup) j++;
  924.       }
  925.  
  926.       if (j>1) {  /* remove target cell from group */
  927.     cellgroup[cnum] = 0;
  928.     selectCell(cnum,0);
  929.     mode = DELONLY;
  930.     if (cnum==editColor) { /* set editColor to first cell in group */
  931.       for (i=0; i<numcols && cellgroup[i]!=curgroup; i++);
  932.       if (i<numcols) editColor = i;
  933.     }
  934.       }
  935.       else {  /* last cell in group.  set to group=0, but don't unhighlight */
  936.     cellgroup[cnum] = 0;
  937.     curgroup = 0;
  938.       }
  939.     }
  940.  
  941.     else if ((mode!=DELONLY) && cellgroup[cnum] != curgroup && 
  942.          cellgroup[cnum]>0) {
  943.       /* merge clicked-on group into curgroup */
  944.       mode = ADDONLY;
  945.       rv = 1;  j = cellgroup[cnum];
  946.       for (i=0; i<numcols; i++) {
  947.     if (cellgroup[i] == j) {
  948.       cellgroup[i] = curgroup;
  949.       selectCell(i,1);
  950.       rcmap[i] = rcmap[editColor];
  951.       gcmap[i] = gcmap[editColor];
  952.       bcmap[i] = bcmap[editColor]; 
  953.     }
  954.       }
  955.     }
  956.         
  957.     else if ((mode!=DELONLY) && cellgroup[cnum] == 0) {
  958.       /* merge clicked-on cell into curgroup */
  959.       mode = ADDONLY;
  960.       rv = 1;
  961.       cellgroup[cnum] = curgroup;
  962.       selectCell(cnum,1);
  963.       rcmap[cnum] = rcmap[editColor];
  964.       gcmap[cnum] = gcmap[editColor];
  965.       bcmap[cnum] = bcmap[editColor]; 
  966.     }
  967.   }
  968.  
  969.   else {  /* !curgroup */
  970.     if ((mode!=DELONLY) && cellgroup[cnum] != 0) {
  971.       /* merge editColor into clicked-on group */
  972.       /* clicked on group, however, takes values of editCell */
  973.       mode = ADDONLY;
  974.       rv = 1;  j = cellgroup[cnum];
  975.       for (i=0; i<numcols; i++) {
  976.     if (cellgroup[i] == j) {
  977.       selectCell(i,1);
  978.       rcmap[i] = rcmap[editColor];
  979.       gcmap[i] = gcmap[editColor];
  980.       bcmap[i] = bcmap[editColor]; 
  981.     }
  982.       }
  983.       curgroup = cellgroup[cnum];
  984.       cellgroup[editColor] = curgroup;
  985.     }
  986.         
  987.     else if ((mode!=DELONLY) && (cellgroup[cnum] == 0) 
  988.          && (cnum != editColor)) {
  989.       /* create new group for these two cells (cnum and editColor) */
  990.       mode = ADDONLY;
  991.       rv = 1;
  992.       maxgroup++;
  993.       cellgroup[cnum] = cellgroup[editColor] = maxgroup;
  994.       selectCell(cnum,1);
  995.       curgroup = maxgroup;
  996.       rcmap[cnum] = rcmap[editColor];
  997.       gcmap[cnum] = gcmap[editColor];
  998.       bcmap[cnum] = bcmap[editColor];
  999.     }
  1000.   }
  1001.  
  1002.   return rv;
  1003. }        
  1004.         
  1005.  
  1006. /*********************/
  1007. void ChangeEC(num)
  1008. int num;
  1009. {
  1010.   /* given a color # that is to become the new editColor, do all 
  1011.      highlighting/unhighlighting, copy editColor's rgb values to
  1012.      the rgb/hsv dials */
  1013.  
  1014.   int i,oldgroup;
  1015.  
  1016.   oldgroup = curgroup;
  1017.  
  1018.   if (curgroup && cellgroup[num] != curgroup) {
  1019.     /* a group is currently selected, and we're picking a new cell that
  1020.        isn't in the group, have to unhighlight entire group */
  1021.  
  1022.     for (i=0; i<numcols; i++) {
  1023.       if (cellgroup[i] == curgroup) selectCell(i,0);
  1024.     }
  1025.   }
  1026.   else if (!curgroup) selectCell(editColor,0);
  1027.  
  1028.   editColor = num;
  1029.   curgroup = cellgroup[editColor];
  1030.  
  1031.   if (curgroup && curgroup != oldgroup) {
  1032.     /* if new cell is in a group, highlight that group */
  1033.     for (i=0; i<numcols; i++) {
  1034.       if (cellgroup[i] == curgroup) selectCell(i,1);
  1035.     }
  1036.   }
  1037.   else if (!curgroup) selectCell(editColor,1);
  1038.  
  1039.  
  1040.   if (hsvmode) {
  1041.     double h, s, v;
  1042.     rgb2hsv(rcmap[editColor], gcmap[editColor], bcmap[editColor], &h, &s, &v);
  1043.     if (h<0) h = 0;
  1044.  
  1045.     DSetVal(&rhDial, (int) h);
  1046.     DSetVal(&gsDial, (int) (s*100));
  1047.     DSetVal(&bvDial, (int) (v*100));
  1048.   }
  1049.   else {
  1050.     DSetVal(&rhDial, rcmap[editColor]);
  1051.     DSetVal(&gsDial, gcmap[editColor]);
  1052.     DSetVal(&bvDial, bcmap[editColor]);
  1053.   }
  1054. }
  1055.   
  1056.     
  1057. /*********************/
  1058. void ApplyECctrls()
  1059. {
  1060.   /* sets values of {r,g,b}cmap[editColor] based on dial settings */
  1061.  
  1062.   if (hsvmode) {
  1063.     int rv, gv, bv;
  1064.     hsv2rgb((double) rhDial.val, ((double) gsDial.val) / 100.0, 
  1065.         ((double) bvDial.val) / 100.0, &rv, &gv, &bv);
  1066.     rcmap[editColor] = rv;
  1067.     gcmap[editColor] = gv;
  1068.     bcmap[editColor] = bv;
  1069.   }
  1070.   else {
  1071.     rcmap[editColor] = rhDial.val;
  1072.     gcmap[editColor] = gsDial.val;
  1073.     bcmap[editColor] = bvDial.val;
  1074.   }
  1075. }
  1076.  
  1077.  
  1078.  
  1079. /*********************/
  1080. void GenerateFSGamma()
  1081. {
  1082.   /* this function generates the Floyd-Steinberg gamma curve (fsgamcr)
  1083.  
  1084.      This function generates a 4 point spline curve to be used as a 
  1085.      non-linear grey 'colormap'.  Two of the points are nailed down at 0,0
  1086.      and 255,255, and can't be changed.  You specify the other two.  If
  1087.      you specify points on the line (0,0 - 255,255), you'll get the normal
  1088.      linear reponse curve.  If you specify points of 50,0 and 200,255, you'll
  1089.      get grey values of 0-50 to map to black (0), and grey values of 200-255
  1090.      to map to white (255) (roughly).  Values between 50 and 200 will cover
  1091.      the output range 0-255.  The reponse curve will be slightly 's' shaped. */
  1092.  
  1093.   int i,j;
  1094.   static int x[4] = {0,32,224,255};
  1095.   static int y[4] = {0, 0,255,255};
  1096.   double yf[4];
  1097.  
  1098.   InitSpline(x, y, 4, yf);
  1099.   
  1100.   for (i=0; i<256; i++) {
  1101.     j = (int) EvalSpline(x, y, yf, 4, (double) i);
  1102.     if (j<0) j=0;
  1103.     else if (j>255) j=255;
  1104.     fsgamcr[i] = j;
  1105.   }
  1106. }
  1107.  
  1108.  
  1109. /*********************/
  1110. static void doCmd(cmd)
  1111. int cmd;
  1112. {
  1113.   int i;
  1114.   GRAF_STATE gs;
  1115.  
  1116.   switch (cmd) {
  1117.  
  1118.   case G_BAPPLY: 
  1119.     if (enabCB.val != 1) { enabCB.val = 1;  CBRedraw(&enabCB); }
  1120.     applyGamma();           
  1121.     break;
  1122.  
  1123.   case G_BNOGAM:
  1124.     if (enabCB.val != 0) { enabCB.val = 0;  CBRedraw(&enabCB); }
  1125.     applyGamma();           
  1126.     break;
  1127.  
  1128.   case G_BRESET: restore_gstate(&defstate);  break;
  1129.   case G_BUNDO:  gamUndo();  break;
  1130.   case G_BREDO:  gamRedo();  break;
  1131.   case G_BCLOSE: GamBox(0);  break;
  1132.  
  1133.  
  1134.   case G_BDN_BR: 
  1135.   case G_BUP_BR: GetGrafState(&intGraf, &gs);
  1136.                  for (i=0; i < gs.nhands; i++) {
  1137.            if (cmd==G_BUP_BR) gs.hands[i].y += 5;
  1138.                          else gs.hands[i].y -= 5;
  1139.            RANGE(gs.hands[i].y, 0, 127);
  1140.          }
  1141.                  SetGrafState(&intGraf, &gs);
  1142.                  changedGam();
  1143.                  break;
  1144.  
  1145.  
  1146.   case G_BUP_CN: GetGrafState(&intGraf, &gs);
  1147.                  for (i=0; i < gs.nhands; i++) {
  1148.            if (gs.hands[i].x < 64) {
  1149.              gs.hands[i].y -= 5;
  1150.            }
  1151.            else {
  1152.              gs.hands[i].y += 5;
  1153.            }
  1154.            RANGE(gs.hands[i].y, 0, 127);
  1155.          }
  1156.                  SetGrafState(&intGraf, &gs);
  1157.                  changedGam();
  1158.                  break;
  1159.  
  1160.   case G_BDN_CN: GetGrafState(&intGraf, &gs);
  1161.                  for (i=0; i < gs.nhands; i++) {
  1162.            if (gs.hands[i].y < 64) {
  1163.              gs.hands[i].y += 5;
  1164.              if (gs.hands[i].y > 64) gs.hands[i].y = 64;
  1165.            }
  1166.            else {
  1167.              gs.hands[i].y -= 5;
  1168.              if (gs.hands[i].y < 64) gs.hands[i].y = 64;
  1169.            }
  1170.          }
  1171.                  SetGrafState(&intGraf, &gs);
  1172.                  changedGam();
  1173.                  break;
  1174.  
  1175.  
  1176.   case G_BMAXCONT: { int minv, maxv, v;
  1177.              minv = 254;  maxv = 1;
  1178.              for (i=0; i<numcols; i++) {
  1179.                v = MONO(rcmap[i],gcmap[i],bcmap[i]);
  1180.                if (v<10 || v>245) continue;  /* 'black' and 'white' */
  1181.                if (v<minv) minv = v;
  1182.                if (v>maxv) maxv = v;
  1183.              }
  1184.  
  1185.              if (minv>=maxv) { /* only found black&white in colormap */
  1186.                minv = 0;  maxv = 255;
  1187.              }
  1188.  
  1189.              gs.spline = 0;
  1190.              gs.entergamma = 0;
  1191.              gs.gammamode = 0;
  1192.              gs.nhands = 4;
  1193.              gs.hands[0].x = 0;       gs.hands[0].y = 0;
  1194.              gs.hands[1].x = minv/2;  gs.hands[1].y = 0;
  1195.              gs.hands[2].x = maxv/2;  gs.hands[2].y = 255/2;
  1196.              gs.hands[3].x = 255/2;   gs.hands[3].y = 255/2;
  1197.  
  1198.              if (minv<2)       { gs.hands[1].x = gs.hands[1].y = 1; }
  1199.              if ((maxv/2)>126) { gs.hands[2].x = gs.hands[2].y = 126; }
  1200.  
  1201.              SetGrafState(&intGraf, &gs);
  1202.              changedGam();
  1203.            }
  1204.                    break;
  1205.  
  1206.   case G_B1:
  1207.   case G_B2:
  1208.   case G_B3:
  1209.   case G_B4: { int p = 0;
  1210.            if      (cmd==G_B1) p = 0;
  1211.            else if (cmd==G_B2) p = 1;
  1212.            else if (cmd==G_B3) p = 2;
  1213.            else if (cmd==G_B4) p = 3;
  1214.                 
  1215.            if (gbut[G_BSET].lit) {
  1216.          save_gstate(&preset[p]);
  1217.          gbut[G_BSET].lit = 0;
  1218.          BTRedraw(&gbut[G_BSET]);
  1219.            }
  1220.            else restore_gstate(&preset[p]);
  1221.          }
  1222.              break;
  1223.  
  1224.   case G_BSET:  break;
  1225.  
  1226.  
  1227.   case G_BHSVRGB:
  1228.     hsvmode = !hsvmode;
  1229.     SetHSVmode();
  1230.     saveGamState();
  1231.     break;
  1232.  
  1233.  
  1234.   case G_BCOLREV: 
  1235.     {
  1236.       struct cmapstate tmp1cmap;
  1237.       int gchg;
  1238.  
  1239.       for (i=0; i<numcols && prevcmap.cellgroup[i]==0; i++);
  1240.       gchg = (i!=numcols);
  1241.  
  1242.       saveCMap(&tmpcmap);         /* buffer current cmapstate */
  1243.     
  1244.       for (i=0; i<numcols; i++) { /* do reversion */
  1245.     rcmap[i] = rorg[i];  
  1246.     gcmap[i] = gorg[i];
  1247.     bcmap[i] = borg[i];
  1248.     cellgroup[i] = 0;
  1249.       }
  1250.       curgroup = maxgroup = 0;
  1251.  
  1252.       saveCMap(&tmp1cmap);        /* buffer current cmapstate */
  1253.     
  1254.       /* prevent multiple 'Undo All's from filling Undo buffer */
  1255.       if (bcmp(&tmpcmap, &tmp1cmap, sizeof(struct cmapstate))) {
  1256.     /* the reversion changed the cmapstate */
  1257.     bcopy(&tmpcmap, &prevcmap, sizeof(struct cmapstate));
  1258.     BTSetActive(&gbut[G_BCOLUNDO],1);
  1259.  
  1260.     RedrawCMap();
  1261.     ChangeEC(editColor);
  1262.     applyGamma();
  1263.     if (gchg) ApplyEditColor(1);
  1264.       }
  1265.     }
  1266.     break;
  1267.  
  1268.  
  1269.   case G_BRNDCOL:
  1270.     saveCMap(&prevcmap);
  1271.     BTSetActive(&gbut[G_BCOLUNDO],1);
  1272.     rndCols();
  1273.     break;
  1274.   
  1275.   case G_BRV:
  1276.     saveCMap(&prevcmap);
  1277.     BTSetActive(&gbut[G_BCOLUNDO],1);
  1278.  
  1279.     if (hsvmode) {              /* reverse video in HSV space (flip V only) */
  1280.       double h,s,v;   int rr, gr, br;
  1281.       for (i=0; i<numcols; i++) {
  1282.     rgb2hsv(rcmap[i], gcmap[i], bcmap[i], &h, &s, &v);
  1283.  
  1284.     v = 1.0 - v;
  1285.     if (v <= .05) v = .05;
  1286.  
  1287.     hsv2rgb(h, s, v, &rr, &gr, &br);
  1288.     rcmap[i] = rr;  gcmap[i] = gr;  bcmap[i] = br;
  1289.       }
  1290.     }
  1291.     else {
  1292.       for (i=0; i<numcols; i++) {
  1293.     rcmap[i] = 255-rcmap[i];
  1294.     gcmap[i] = 255-gcmap[i];
  1295.     bcmap[i] = 255-bcmap[i];
  1296.       }
  1297.     }
  1298.     ChangeEC(editColor);
  1299.     applyGamma();
  1300.     break;
  1301.   
  1302.  
  1303.   case G_BMONO:
  1304.     saveCMap(&prevcmap);
  1305.     BTSetActive(&gbut[G_BCOLUNDO],1);
  1306.     for (i=0; i<numcols; i++) {
  1307.       rcmap[i] = gcmap[i] = bcmap[i] = MONO(rcmap[i],gcmap[i],bcmap[i]);
  1308.     }
  1309.     ChangeEC(editColor);
  1310.     applyGamma();
  1311.     break;
  1312.   
  1313.  
  1314.   case G_BCOLUNDO:
  1315.     for (i=0; i<numcols && cellgroup[i]==prevcmap.cellgroup[i]; i++);
  1316.  
  1317.     saveCMap(&tmpcmap);
  1318.     restoreCMap(&prevcmap);
  1319.     bcopy(&tmpcmap, &prevcmap, sizeof(struct cmapstate));
  1320.     RedrawCMap();
  1321.     ChangeEC(editColor);
  1322.     applyGamma();
  1323.     if (i!=numcols) ApplyEditColor(1);
  1324.     break;
  1325.  
  1326.   case G_BGETRES:   makeResources();  break;
  1327.   }
  1328. }
  1329.  
  1330.  
  1331. /*********************/
  1332. static void SetHSVmode()
  1333. {
  1334.   if (!hsvmode) {
  1335.     rhDial.title = "Red";
  1336.     gsDial.title = "Green";
  1337.     bvDial.title = "Blue";
  1338.            
  1339.     DSetRange(&rhDial, 0, 255, rcmap[editColor], 16);
  1340.     DSetRange(&gsDial, 0, 255, gcmap[editColor], 16);
  1341.     DSetRange(&bvDial, 0, 255, bcmap[editColor], 16);
  1342.  
  1343.     XClearWindow(theDisp, rhDial.win);    DRedraw(&rhDial);
  1344.     XClearWindow(theDisp, gsDial.win);    DRedraw(&gsDial);
  1345.     XClearWindow(theDisp, bvDial.win);    DRedraw(&bvDial);
  1346.   }
  1347.  
  1348.   else {
  1349.     double h,s,v;
  1350.  
  1351.     rhDial.title = "Hue";
  1352.     gsDial.title = "Sat.";
  1353.     bvDial.title = "Value";
  1354.  
  1355.     rgb2hsv(rcmap[editColor], gcmap[editColor], bcmap[editColor],
  1356.         &h, &s, &v);
  1357.  
  1358.     if (h<0.0) h = 0.0;
  1359.     DSetRange(&rhDial, 0, 360, (int) h, 5);
  1360.     DSetRange(&gsDial, 0, 100, (int) (s*100), 5);
  1361.     DSetRange(&bvDial, 0, 100, (int) (v*100), 5);
  1362.  
  1363.     XClearWindow(theDisp, rhDial.win);    DRedraw(&rhDial);
  1364.     XClearWindow(theDisp, gsDial.win);    DRedraw(&gsDial);
  1365.     XClearWindow(theDisp, bvDial.win);    DRedraw(&bvDial);
  1366.   }
  1367. }
  1368.  
  1369.  
  1370. /*********************/
  1371. static void applyGamma()
  1372. {
  1373.   /* called to regenerate the image based on r/g/bcmap or output of rgb/hsv
  1374.      filters.  Doesn't check autoCB.  */
  1375.  
  1376.   int i,j;
  1377.   byte oldr[256], oldg[256], oldb[256];
  1378.  
  1379.   /* save current 'desired' colormap */
  1380.   bcopy(r, oldr, numcols);
  1381.   bcopy(g, oldg, numcols);
  1382.   bcopy(b, oldb, numcols);
  1383.  
  1384.   GammifyColors();
  1385.  
  1386.   /* if current 'desired' colormap hasn't changed, don't DO anything */
  1387.   if (!bcmp(r, oldr, numcols) && 
  1388.       !bcmp(g, oldg, numcols) && 
  1389.       !bcmp(b, oldb, numcols)) return;
  1390.  
  1391.  
  1392.  
  1393.   /* special case: if using R/W color, just modify the colors and leave */
  1394.   if (rwcolor && rwthistime) {
  1395.     XColor ctab[256];
  1396.  
  1397.     for (i=0; i<nfcols; i++) {
  1398.       j = fc2pcol[i];
  1399.       if (mono) {
  1400.     int intens = MONO(r[j], g[j], b[j]);
  1401.     ctab[i].red = ctab[i].green = ctab[i].blue = intens<<8;
  1402.       }
  1403.       else {
  1404.     ctab[i].red   = r[j]<<8;
  1405.     ctab[i].green = g[j]<<8;
  1406.     ctab[i].blue  = b[j]<<8;
  1407.       }
  1408.  
  1409.       ctab[i].pixel = freecols[i];
  1410.       ctab[i].flags = DoRed | DoGreen | DoBlue;
  1411.       XStoreColor(theDisp, LocalCmap ? LocalCmap : theCmap, &ctab[i]);
  1412.     }
  1413.     XStoreColor(theDisp, LocalCmap ? LocalCmap : theCmap, &ctab[0]);
  1414.  
  1415.     for (i=0; i<numcols; i++) {
  1416.       rdisp[colAllocOrder[i]] = r[rwpc2pc[i]];
  1417.       gdisp[colAllocOrder[i]] = g[rwpc2pc[i]];
  1418.       bdisp[colAllocOrder[i]] = b[rwpc2pc[i]];
  1419.     }
  1420.  
  1421.     return;
  1422.   }
  1423.     
  1424.  
  1425.   FreeAllColors();
  1426.   if (theVisual->class & 1 && ncols>0) XClearWindow(theDisp,mainW);
  1427.  
  1428.   SortColormap();
  1429.   AllocColors();
  1430.   CreateXImage();
  1431.  
  1432.   if (useroot) MakeRootPic();
  1433.           else DrawWindow(0,0,eWIDE,eHIGH);
  1434.   if (but[BCROP].active) InvCropRect();
  1435.   SetCursors(-1);
  1436. }
  1437.  
  1438.  
  1439.  
  1440. /*********************/
  1441. void GammifyColors()
  1442. {
  1443.   int i;
  1444.  
  1445.   if (enabCB.val) {
  1446.     for (i=0; i<numcols; i++) Gammify1(i);
  1447.   }
  1448.   else {
  1449.     for (i=0; i<numcols; i++) { 
  1450.       r[i] = rcmap[i];
  1451.       g[i] = gcmap[i];
  1452.       b[i] = bcmap[i];
  1453.     }
  1454.   }
  1455. }
  1456.  
  1457.  
  1458. #define NOHUE -1
  1459.  
  1460. /*********************/
  1461. void Gammify1(col)
  1462. int col;
  1463. {
  1464.   int rv, gv, bv, vi, hi;
  1465.   double h,s,v;
  1466.  
  1467.   rv = rcmap[col];  gv = gcmap[col];  bv = bcmap[col];
  1468.  
  1469.   rgb2hsv(rv, gv, bv, &h, &s, &v);
  1470.  
  1471.   /* map near-black to black to avoid weird effects */
  1472.   if (v <= .0625) s = 0.0;
  1473.  
  1474.   /* apply intGraf.func[] function to 'v' (the intensity) */
  1475.   vi = (int) floor(v * 255);
  1476.   v = intGraf.func[vi] / 255.0;
  1477.  
  1478.   if (h>=0) {
  1479.     hi = (int) h;  
  1480.     if (hi<0)    hi += 360;
  1481.     if (hi>=360) hi -= 360;
  1482.     h = (double) hremap[hi];
  1483.   }
  1484.   else {
  1485.     if (whtHD.enabCB.val) {
  1486.       h = (double) whtHD.stval;
  1487.       s = (double) whtHD.satval / 100.0;
  1488.  
  1489.       /* special case:  if stval = satval = 0, set hue = -1 */
  1490.       if (whtHD.stval == 0 && whtHD.satval == 0) h = -1.0;
  1491.     }
  1492.   }
  1493.  
  1494.   /* apply satDial value to s */
  1495.   s = s + ((double) satDial.val) / 100.0;
  1496.   if (s<0.0) s = 0.0;
  1497.   if (s>1.0) s = 1.0;
  1498.  
  1499.   hsv2rgb(h,s,v,&rv, &gv, &bv);
  1500.  
  1501.   r[col] = rGraf.func[rv];  
  1502.   g[col] = gGraf.func[gv];
  1503.   b[col] = bGraf.func[bv];
  1504. }
  1505.  
  1506.  
  1507. /*********************/
  1508. void rgb2hsv(r,g,b, hr, sr, vr)
  1509. int r, g, b;
  1510. double *hr, *sr, *vr;
  1511. {
  1512.   double rd, gd, bd, h, s, v, max, min, del, rc, gc, bc;
  1513.  
  1514.   /* convert RGB to HSV */
  1515.   rd = r / 255.0;            /* rd,gd,bd range 0-1 instead of 0-255 */
  1516.   gd = g / 255.0;
  1517.   bd = b / 255.0;
  1518.  
  1519.   /* compute maximum of rd,gd,bd */
  1520.   if (rd>=gd) { if (rd>=bd) max = rd;  else max = bd; }
  1521.          else { if (gd>=bd) max = gd;  else max = bd; }
  1522.  
  1523.   /* compute minimum of rd,gd,bd */
  1524.   if (rd<=gd) { if (rd<=bd) min = rd;  else min = bd; }
  1525.          else { if (gd<=bd) min = gd;  else min = bd; }
  1526.  
  1527.   del = max - min;
  1528.   v = max;
  1529.   if (max != 0.0) s = (del) / max;
  1530.              else s = 0.0;
  1531.  
  1532.   h = NOHUE;
  1533.   if (s != 0.0) {
  1534.     rc = (max - rd) / del;
  1535.     gc = (max - gd) / del;
  1536.     bc = (max - bd) / del;
  1537.  
  1538.     if      (rd==max) h = bc - gc;
  1539.     else if (gd==max) h = 2 + rc - bc;
  1540.     else if (bd==max) h = 4 + gc - rc;
  1541.  
  1542.     h = h * 60;
  1543.     if (h<0) h += 360;
  1544.   }
  1545.  
  1546.   *hr = h;  *sr = s;  *vr = v;
  1547. }
  1548.  
  1549.  
  1550.  
  1551. /*********************/
  1552. void hsv2rgb(h, s, v, rr, gr, br)
  1553. double h, s, v;
  1554. int *rr, *gr, *br;
  1555. {
  1556.   int    j;
  1557.   double rd, gd, bd;
  1558.   double f, p, q, t;
  1559.  
  1560.   /* convert HSV back to RGB */
  1561.   if (h==NOHUE || s==0.0) { rd = v;  gd = v;  bd = v; }
  1562.   else {
  1563.     if (h==360.0) h = 0.0;
  1564.     h = h / 60.0;
  1565.     j = (int) floor(h);
  1566.     f = h - j;
  1567.     p = v * (1-s);
  1568.     q = v * (1 - (s*f));
  1569.     t = v * (1 - (s*(1 - f)));
  1570.  
  1571.     switch (j) {
  1572.     case 0:  rd = v;  gd = t;  bd = p;  break;
  1573.     case 1:  rd = q;  gd = v;  bd = p;  break;
  1574.     case 2:  rd = p;  gd = v;  bd = t;  break;
  1575.     case 3:  rd = p;  gd = q;  bd = v;  break;
  1576.     case 4:  rd = t;  gd = p;  bd = v;  break;
  1577.     case 5:  rd = v;  gd = p;  bd = q;  break;
  1578.     default: rd = v;  gd = t;  bd = p;  break;  /* never happen */
  1579.     }
  1580.   }
  1581.  
  1582.   *rr = (int) floor(rd * 255);
  1583.   *gr = (int) floor(gd * 255);
  1584.   *br = (int) floor(bd * 255);
  1585. }
  1586.  
  1587.  
  1588.  
  1589. static int undoptr = 0;
  1590.  
  1591. /*********************/
  1592. static void save_gstate(gs)
  1593. struct gamstate *gs;
  1594. {
  1595.   bcopy(hmap, gs->hmap, sizeof(hmap));
  1596.  
  1597.   gs->wht_stval = whtHD.stval;  
  1598.   gs->wht_satval = whtHD.satval;  
  1599.   gs->wht_enab = whtHD.enabCB.val;
  1600.  
  1601.   gs->hueRBnum = RBWhich(hueRB);
  1602.  
  1603.   gs->satval = satDial.val;
  1604.   GetGrafState(&intGraf,&gs->istate);
  1605.   GetGrafState(&rGraf,  &gs->rstate);
  1606.   GetGrafState(&gGraf,  &gs->gstate);
  1607.   GetGrafState(&bGraf,  &gs->bstate);
  1608. }
  1609.  
  1610.  
  1611. /*********************/
  1612. static void restore_gstate(gs)
  1613. struct gamstate *gs;
  1614. {
  1615.   int changed = 0;
  1616.   struct hmap *hm;
  1617.  
  1618.   if (gs->hueRBnum != RBWhich(hueRB)) {
  1619.     RBSelect(hueRB, gs->hueRBnum);
  1620.     changed++;
  1621.   }
  1622.  
  1623.   if (bcmp(hmap, gs->hmap, sizeof(hmap))) {   /* hmap has changed */
  1624.     bcopy(gs->hmap, hmap, sizeof(hmap));
  1625.     build_hremap();
  1626.     changed++;
  1627.  
  1628.     hm = &(gs->hmap[gs->hueRBnum]);
  1629.  
  1630.     if (srcHD.stval  != hm->src_st ||
  1631.     srcHD.enval  != hm->src_en ||
  1632.     srcHD.ccwise != hm->src_ccw) {
  1633.       srcHD.stval  = hm->src_st;
  1634.       srcHD.enval  = hm->src_en;
  1635.       srcHD.ccwise = hm->src_ccw;
  1636.       HDRedraw(&srcHD, HD_ALL | HD_CLEAR);
  1637.     }
  1638.     
  1639.     if (dstHD.stval  != hm->dst_st ||
  1640.     dstHD.enval  != hm->dst_en ||
  1641.     dstHD.ccwise != hm->dst_ccw) {
  1642.       dstHD.stval  = hm->dst_st;
  1643.       dstHD.enval  = hm->dst_en;
  1644.       dstHD.ccwise = hm->dst_ccw;
  1645.       HDRedraw(&dstHD, HD_ALL | HD_CLEAR);
  1646.     }
  1647.   }    
  1648.  
  1649.  
  1650.   if (whtHD.stval != gs->wht_stval || whtHD.satval != gs->wht_satval ||
  1651.       whtHD.enabCB.val != gs->wht_enab) {
  1652.     whtHD.stval  = gs->wht_stval;
  1653.     whtHD.satval  = gs->wht_satval;
  1654.     whtHD.enabCB.val = gs->wht_enab;
  1655.     CBRedraw(&whtHD.enabCB);
  1656.     HDRedraw(&whtHD, HD_ALL | HD_CLEAR);
  1657.     changed++;
  1658.   }
  1659.     
  1660.   if (gs->satval != satDial.val) {
  1661.     DSetVal(&satDial,gs->satval);
  1662.     changed++;
  1663.   }
  1664.  
  1665.   if (SetGrafState(&intGraf, &gs->istate)) changed++;
  1666.   if (SetGrafState(&rGraf,   &gs->rstate)) changed++;
  1667.   if (SetGrafState(&gGraf,   &gs->gstate)) changed++;
  1668.   if (SetGrafState(&bGraf,   &gs->bstate)) changed++;
  1669.  
  1670.   if (changed) changedGam();
  1671. }
  1672.  
  1673.  
  1674. static int nosave_kludge = 0;
  1675.  
  1676. /*********************/
  1677. static void saveGamState()
  1678. {
  1679.   /* increment uptr, sticks current state into uptr
  1680.      set utail = uptr
  1681.      If utail==uhead, increment uhead (buffer full, throw away earliest)
  1682.    */
  1683.  
  1684.   if (nosave_kludge) return;
  1685.  
  1686.   uptr = (uptr+1) % MAXUNDO;
  1687.   save_gstate(&undo[uptr]);
  1688.   utail = uptr;
  1689.   if (utail == uhead) uhead = (uhead + 1) % MAXUNDO;
  1690.  
  1691.   BTSetActive(&gbut[G_BUNDO],1);
  1692.   BTSetActive(&gbut[G_BREDO],0);
  1693. }
  1694.  
  1695.  
  1696.  
  1697. /*********************/
  1698. static void gamUndo()
  1699. {
  1700.   /* if uptr!=uhead  decements uptr, restores state pointed to by uptr
  1701.                      if uptr now == uhead, turn off 'Undo' button
  1702.    */
  1703.  
  1704.   if (uptr != uhead) {   /* this should always be true when gamUndo called */
  1705.     uptr = (uptr + MAXUNDO - 1) % MAXUNDO;
  1706.     nosave_kludge = 1;
  1707.     restore_gstate(&undo[uptr]);
  1708.     nosave_kludge = 0;
  1709.     if (uptr == uhead) BTSetActive(&gbut[G_BUNDO],0);
  1710.     if (uptr != utail) BTSetActive(&gbut[G_BREDO],1);
  1711.   }
  1712. }
  1713.  
  1714.  
  1715.  
  1716. /*********************/
  1717. static void gamRedo()
  1718. {
  1719.   /* if uptr != utail   increments uptr, restores state pointed to by uptr
  1720.                         if uptr now == utail, turn off 'Redo' button */
  1721.  
  1722.   if (uptr != utail) {   /* this should always be true when gamRedo called */
  1723.     uptr = (uptr + 1) % MAXUNDO;
  1724.     nosave_kludge = 1;
  1725.     restore_gstate(&undo[uptr]);
  1726.     nosave_kludge = 0;
  1727.     if (uptr != uhead) BTSetActive(&gbut[G_BUNDO],1);
  1728.     if (uptr == utail) BTSetActive(&gbut[G_BREDO],0);
  1729.   }
  1730. }
  1731.  
  1732.  
  1733.  
  1734.  
  1735. /*********************/
  1736. static void rndCols()
  1737. {
  1738.   int i,j;
  1739.  
  1740.   for (i=0; i<numcols; i++) {
  1741.     if (cellgroup[i]) {
  1742.       /* determine if this group's already been given a random color */
  1743.       for (j=0; j<i && cellgroup[i] != cellgroup[j]; j++);
  1744.       if (j<i) {  /* it has */
  1745.     rcmap[i] = rcmap[j];  gcmap[i] = gcmap[j];  bcmap[i] = bcmap[j];
  1746.     continue;
  1747.       }
  1748.     }
  1749.  
  1750.     rcmap[i] = random()&0xff;
  1751.     gcmap[i] = random()&0xff;
  1752.     bcmap[i] = random()&0xff;
  1753.   }
  1754.  
  1755.   ChangeEC(editColor);
  1756.   applyGamma();
  1757. }
  1758.  
  1759.  
  1760.  
  1761. /*********************/
  1762. static void saveCMap(cst)
  1763. struct cmapstate *cst;
  1764. {
  1765.   int i;
  1766.  
  1767.   for (i=0; i<256; i++) {
  1768.     cst->r[i] = rcmap[i];
  1769.     cst->g[i] = gcmap[i];
  1770.     cst->b[i] = bcmap[i];
  1771.     cst->cellgroup[i] = cellgroup[i];
  1772.   }
  1773.  
  1774.   cst->curgroup  = curgroup;
  1775.   cst->maxgroup  = maxgroup;
  1776.   cst->editColor = editColor;
  1777. }
  1778.  
  1779.  
  1780. /*********************/
  1781. static void restoreCMap(cst)
  1782. struct cmapstate *cst;
  1783. {
  1784.   int i;
  1785.  
  1786.   for (i=0; i<256; i++) {
  1787.     rcmap[i] = cst->r[i];
  1788.     gcmap[i] = cst->g[i];
  1789.     bcmap[i] = cst->b[i];
  1790.     cellgroup[i] = cst->cellgroup[i];
  1791.   }
  1792.  
  1793.   curgroup = cst->curgroup;
  1794.   maxgroup = cst->maxgroup;
  1795.   editColor = cst->editColor;
  1796. }
  1797.  
  1798.  
  1799.     
  1800.  
  1801. /*********************/
  1802. static void parseResources()
  1803. {
  1804.   char gname[80], tmp[80], tmp1[256];
  1805.   int  i,j;
  1806.   struct gamstate *gsp, gs;
  1807.  
  1808.   for (i=0; i<5; i++) {
  1809.     if (i) { sprintf(gname,"preset%d",i);  gsp = &preset[i-1]; }
  1810.       else { sprintf(gname,"default");     gsp = &defstate; }
  1811.  
  1812.     bcopy(gsp, &gs, sizeof(struct gamstate));   /* load 'gs' with defaults */
  1813.  
  1814.     for (j=0; j<6; j++) {                       /* xv.*.huemap resources */
  1815.       sprintf(tmp, "%s.huemap%d", gname, j+1);
  1816.       if (rd_str(tmp)) {                        /* got one */
  1817.     int fst, fen, tst, ten;
  1818.     char fcw[32], tcw[32];
  1819.  
  1820.     if (DEBUG) fprintf(stderr,"parseResource 'xv.%s: %s'\n",tmp, def_str);
  1821.     lower_str(def_str);
  1822.     if (sscanf(def_str,"%d %d %s %d %d %s",
  1823.            &fst, &fen, fcw, &tst, &ten, tcw) != 6) {
  1824.       fprintf(stderr,"%s: unable to parse resource 'xv.%s: %s'\n", 
  1825.           cmd, tmp, def_str);
  1826.     }
  1827.     else {
  1828.       RANGE(fst, 0, 359);   RANGE(fen, 0, 359);
  1829.       RANGE(tst, 0, 359);   RANGE(ten, 0, 359);
  1830.       gs.hmap[j].src_st  = fst;
  1831.       gs.hmap[j].src_en  = fen;
  1832.       gs.hmap[j].src_ccw = (strcmp(fcw,"ccw") == 0);
  1833.       gs.hmap[j].dst_st  = tst;
  1834.       gs.hmap[j].dst_en  = ten;
  1835.       gs.hmap[j].dst_ccw = (strcmp(tcw,"ccw") == 0);
  1836.     }
  1837.       }
  1838.     }
  1839.  
  1840.     sprintf(tmp, "%s.whtmap", gname);           /* xv.*.whtmap resource */
  1841.     if (rd_str(tmp)) {                          /* got one */
  1842.       int wst, wsat, enab;
  1843.       if (DEBUG) fprintf(stderr,"parseResource 'xv.%s: %s'\n",tmp, def_str);
  1844.       if (sscanf(def_str,"%d %d %d", &wst, &wsat, &enab) != 3) {
  1845.     fprintf(stderr,"%s: unable to parse resource 'xv.%s: %s'\n", 
  1846.         cmd, tmp, def_str);
  1847.       }
  1848.       else {                                    /* successful parse */
  1849.     RANGE(wst, 0, 359);  RANGE(wsat, 0, 100);
  1850.     gs.wht_stval  = wst;
  1851.     gs.wht_satval = wsat;
  1852.     gs.wht_enab   = enab;
  1853.       }
  1854.     }
  1855.  
  1856.     sprintf(tmp, "%s.satval", gname);           /* xv.*.satval resource */
  1857.     if (rd_str(tmp)) {                          /* got one */
  1858.       int sat;
  1859.       if (DEBUG) fprintf(stderr,"parseResource 'xv.%s: %s'\n",tmp, def_str);
  1860.       if (sscanf(def_str,"%d", &sat) != 1) {
  1861.     fprintf(stderr,"%s: unable to parse resource 'xv.%s: %s'\n", 
  1862.         cmd, tmp, def_str);
  1863.       }
  1864.       else {                                    /* successful parse */
  1865.     RANGE(sat, -100, 100);
  1866.     gs.satval = sat;
  1867.       }
  1868.     }
  1869.  
  1870.     for (j=0; j<4; j++) {                       /* xv.*.*graf resources */
  1871.       GRAF_STATE gstat, *gsgst;
  1872.       switch (j) {
  1873.       case 0: sprintf(tmp, "%s.igraf", gname);  gsgst = &gs.istate; break;
  1874.       case 1: sprintf(tmp, "%s.rgraf", gname);  gsgst = &gs.rstate; break;
  1875.       case 2: sprintf(tmp, "%s.ggraf", gname);  gsgst = &gs.gstate; break;
  1876.       case 3: sprintf(tmp, "%s.bgraf", gname);  gsgst = &gs.bstate; break;
  1877.       default: sprintf(tmp, "%s.bgraf", gname);  gsgst = &gs.bstate; break;
  1878.       }
  1879.  
  1880.       if (rd_str(tmp)) {                        /* got one */
  1881.     strcpy(tmp1, def_str);
  1882.     bcopy(gsgst, &gstat, sizeof(GRAF_STATE));
  1883.     if (DEBUG) fprintf(stderr,"parseResource 'xv.%s: %s'\n",tmp, tmp1);
  1884.     if (!Str2Graf(&gstat, tmp1)) {            /* successful parse */
  1885.       bcopy(&gstat, gsgst, sizeof(GRAF_STATE));
  1886.     }
  1887.       }
  1888.     }
  1889.     
  1890.     /* copy (potentially) modified gs back to default/preset */
  1891.     bcopy(&gs, gsp, sizeof(struct gamstate));
  1892.   }
  1893. }
  1894.  
  1895.  
  1896. /*********************/
  1897. static void makeResources()
  1898. {
  1899.   char rsrc[2000];     /* wild over-estimation */
  1900.   char gname[40], rname[64], tmp[256], tmp1[256];
  1901.   struct gamstate gstate;
  1902.   int i;
  1903.  
  1904.   rsrc[0] = '\0';
  1905.  
  1906.   /* write out current state */
  1907.   save_gstate(&gstate);
  1908.   strcpy(gname, "xv.default");
  1909.   
  1910.   /* write out huemap resources */
  1911.   for (i=0; i<6; i++) {
  1912.     if (1 || gstate.hmap[i].src_st  != gstate.hmap[i].dst_st ||
  1913.     gstate.hmap[i].src_en  != gstate.hmap[i].dst_en ||
  1914.     gstate.hmap[i].src_ccw != gstate.hmap[i].dst_ccw) {
  1915.       sprintf(tmp, "%s.huemap%d: %3d %3d %3s %3d %3d %3s\n", gname, i+1, 
  1916.           gstate.hmap[i].src_st, gstate.hmap[i].src_en, 
  1917.           gstate.hmap[i].src_ccw ? "CCW" : "CW",
  1918.           gstate.hmap[i].dst_st, gstate.hmap[i].dst_en, 
  1919.           gstate.hmap[i].dst_ccw ? "CCW" : "CW");
  1920.       strcat(rsrc, tmp);
  1921.     }
  1922.   }
  1923.  
  1924.   /* write out whtmap resource */
  1925.   if (1 || gstate.wht_stval || gstate.wht_satval || gstate.wht_enab != 1) {
  1926.     sprintf(tmp, "%s.whtmap:  %d %d %d\n", gname, gstate.wht_stval, 
  1927.         gstate.wht_satval, gstate.wht_enab);
  1928.     strcat(rsrc, tmp);
  1929.   }
  1930.  
  1931.   /* write out satval resource */
  1932.   if (1 || gstate.satval) {
  1933.     sprintf(tmp, "%s.satval:  %d\n", gname, gstate.satval);
  1934.     strcat(rsrc, tmp);
  1935.   }
  1936.  
  1937.  
  1938.   /* write out graf resources */
  1939.   for (i=0; i<4; i++) {
  1940.     GRAF_STATE *gfstat;
  1941.     switch (i) {
  1942.     case 0: sprintf(rname, "%s.igraf", gname);  gfstat = &gstate.istate; break;
  1943.     case 1: sprintf(rname, "%s.rgraf", gname);  gfstat = &gstate.rstate; break;
  1944.     case 2: sprintf(rname, "%s.ggraf", gname);  gfstat = &gstate.gstate; break;
  1945.     case 3: sprintf(rname, "%s.bgraf", gname);  gfstat = &gstate.bstate; break;
  1946.     default:
  1947.       sprintf(rname, "%s.bgraf", gname);  gfstat = &gstate.bstate; break;
  1948.     }
  1949.  
  1950.     Graf2Str(gfstat, tmp1);
  1951.     sprintf(tmp, "%s: %s\n", rname, tmp1);
  1952.     strcat(rsrc, tmp);
  1953.   }
  1954.  
  1955.   XStoreBytes(theDisp, rsrc, strlen(rsrc));
  1956. }
  1957.     
  1958.     
  1959.     
  1960.         
  1961.  
  1962. /**********************************************/
  1963. /*************  HUE wheel functions ***********/
  1964. /**********************************************/
  1965.  
  1966.  
  1967. static int hdb_pixmaps_built = 0;
  1968. static Pixmap hdbpix1[N_HDBUTT];
  1969. static Pixmap hdbpix2[N_HDBUTT2];
  1970. #define PW 15
  1971. #define PH 15
  1972.  
  1973. /**************************************************/
  1974. static void HDCreate(hd, win, x, y, r, st, en, ccwise, str, fg, bg)
  1975. HDIAL *hd;
  1976. Window win;
  1977. int x,y,r,st,en,ccwise;
  1978. char *str;
  1979. u_long fg,bg;
  1980. {
  1981.   int i;
  1982.  
  1983.   hd->win    = win;
  1984.   hd->x      = x;
  1985.   hd->y      = y;
  1986.   hd->range  = r;
  1987.   hd->stval  = st;
  1988.   hd->enval  = en;
  1989.   hd->satval = 0;
  1990.   hd->ccwise = ccwise;
  1991.   hd->str    = str;
  1992.   hd->fg     = fg;
  1993.   hd->bg     = bg;
  1994.  
  1995.   if (!hdb_pixmaps_built) {
  1996.     hdbpix1[HDB_ROTL]   = XCreatePixmapFromBitmapData(theDisp, win,
  1997.                                     h_rotl_bits, PW,PH, 1, 0, 1);
  1998.     hdbpix1[HDB_ROTR]   = XCreatePixmapFromBitmapData(theDisp, win,
  1999.                                     h_rotr_bits, PW,PH, 1, 0, 1);
  2000.     hdbpix1[HDB_FLIP]   = XCreatePixmapFromBitmapData(theDisp, win,
  2001.                                     h_flip_bits, PW,PH, 1, 0, 1);
  2002.     hdbpix1[HDB_EXPND]  = XCreatePixmapFromBitmapData(theDisp, win,
  2003.                                     h_sinc_bits, PW,PH, 1, 0, 1);
  2004.     hdbpix1[HDB_SHRNK]  = XCreatePixmapFromBitmapData(theDisp, win,
  2005.                                     h_sdec_bits, PW,PH, 1, 0, 1);
  2006.  
  2007.     hdbpix2[HDB_SAT]    = XCreatePixmapFromBitmapData(theDisp, win,
  2008.                                     h_sat_bits, PW,PH, 1, 0, 1);
  2009.     hdbpix2[HDB_DESAT]  = XCreatePixmapFromBitmapData(theDisp, win,
  2010.                                     h_desat_bits, PW,PH, 1, 0, 1);
  2011.  
  2012.     hdbpix2[HDB_ROTL]  = hdbpix1[HDB_ROTL];
  2013.     hdbpix2[HDB_ROTR]  = hdbpix1[HDB_ROTR];
  2014.   }
  2015.  
  2016.     
  2017.   if (hd->range) {
  2018.     BTCreate(&hd->hdbutt[HDB_ROTL], win, x-50,y+60,18,18,NULL,fg,bg);
  2019.     BTCreate(&hd->hdbutt[HDB_ROTR], win, x-30,y+60,18,18,NULL,fg,bg);
  2020.     BTCreate(&hd->hdbutt[HDB_FLIP], win, x-10,y+60,18,18,NULL,fg,bg);
  2021.     BTCreate(&hd->hdbutt[HDB_EXPND],win, x+10,y+60,18,18,NULL,fg,bg);
  2022.     BTCreate(&hd->hdbutt[HDB_SHRNK],win, x+30,y+60,18,18,NULL,fg,bg);
  2023.  
  2024.     for (i=0; i<N_HDBUTT; i++) {
  2025.       hd->hdbutt[i].pix = hdbpix1[i];
  2026.       hd->hdbutt[i].pw = PW;
  2027.       hd->hdbutt[i].ph = PH;
  2028.     }
  2029.   }
  2030.  
  2031.   else {
  2032.     BTCreate(&hd->hdbutt[HDB_ROTL], win, x-39,y+60,18,18,NULL,fg,bg);
  2033.     BTCreate(&hd->hdbutt[HDB_ROTR], win, x-19,y+60,18,18,NULL,fg,bg);
  2034.     BTCreate(&hd->hdbutt[HDB_DESAT],win, x+1, y+60,18,18,NULL,fg,bg);
  2035.     BTCreate(&hd->hdbutt[HDB_SAT],  win, x+21,y+60,18,18,NULL,fg,bg);
  2036.     CBCreate(&hd->enabCB, win, x+23, y-44, "", fg, bg);
  2037.     hd->enabCB.val = 1;
  2038.  
  2039.     for (i=0; i<N_HDBUTT2; i++) {
  2040.       hd->hdbutt[i].pix = hdbpix2[i];
  2041.       hd->hdbutt[i].pw = PW;
  2042.       hd->hdbutt[i].ph = PH;
  2043.     }
  2044.   }
  2045. }
  2046.  
  2047.  
  2048. /**************************************************/
  2049. static void HDRedraw(hd, flags)
  2050. HDIAL *hd;
  2051. int flags;
  2052. {
  2053.   int    i, x, y, x1, y1;
  2054.   double a;
  2055.  
  2056.   if (flags & HD_CLEAR) {
  2057.     XSetForeground(theDisp, theGC, hd->bg);
  2058.     XFillArc(theDisp, hd->win, theGC, hd->x-(HD_RADIUS-1),hd->y-(HD_RADIUS-1),
  2059.          (HD_RADIUS-1)*2, (HD_RADIUS-1)*2, 0, 360*64);
  2060.   }
  2061.  
  2062.   if (flags & HD_FRAME) {
  2063.     static char *colstr = "RYGCBM";
  2064.     char tstr[2];
  2065.  
  2066.     XSetForeground(theDisp, theGC, hd->fg);
  2067.     XDrawArc(theDisp, hd->win, theGC, hd->x - HD_RADIUS, hd->y - HD_RADIUS,
  2068.          HD_RADIUS*2, HD_RADIUS*2, 0, 360*64);
  2069.     
  2070.     for (i=0; i<6; i++) {
  2071.       int kldg;
  2072.  
  2073.       if (i==2 || i==5) kldg = -1;  else kldg = 0;
  2074.       a = hdg2xdg(i*60) * DEG2RAD;
  2075.       pol2xy(hd->x, hd->y, a, HD_RADIUS+1,      &x,  &y);
  2076.       pol2xy(hd->x, hd->y, a, HD_RADIUS+4+kldg, &x1, &y1);
  2077.       XDrawLine(theDisp, hd->win, theGC, x,y,x1,y1);
  2078.  
  2079.       tstr[0] = colstr[i];  tstr[1] = '\0';
  2080.       pol2xy(hd->x, hd->y, a, HD_RADIUS+10, &x, &y);
  2081.       CenterString(hd->win, tstr, x,y);
  2082.     }
  2083.   }
  2084.  
  2085.   if (flags & HD_HANDS || flags & HD_CLHNDS) {
  2086.     if (flags & HD_CLHNDS) XSetForeground(theDisp, theGC, hd->bg);
  2087.                       else XSetForeground(theDisp, theGC, hd->fg);
  2088.  
  2089.     if (hd->range ) {
  2090.       if (flags & HD_HANDS)   /* draw center dot */
  2091.     XFillRectangle(theDisp, hd->win, theGC, hd->x-1, hd->y-1, 3,3);
  2092.  
  2093.       a = hdg2xdg(hd->stval) * DEG2RAD;
  2094.       pol2xy(hd->x, hd->y, a, HD_RADIUS - 4, &x, &y);
  2095.       XDrawLine(theDisp, hd->win, theGC, hd->x, hd->y, x,y);
  2096.       
  2097.       if (flags & HD_CLHNDS) 
  2098.     XFillRectangle(theDisp, hd->win, theGC, x-2,y-2, 5,5);
  2099.       else {
  2100.     XSetForeground(theDisp, theGC, hd->bg);
  2101.     XFillRectangle(theDisp, hd->win, theGC, x-1, y-1, 3,3);
  2102.     XSetForeground(theDisp, theGC, hd->fg);
  2103.     XDrawPoint(theDisp, hd->win, theGC, x, y);
  2104.     XDrawRectangle(theDisp, hd->win, theGC, x-2, y-2, 4,4);
  2105.       }
  2106.  
  2107.       a = hdg2xdg(hd->enval) * DEG2RAD;
  2108.       pol2xy(hd->x, hd->y, a, HD_RADIUS - 4, &x, &y);
  2109.       XDrawLine(theDisp, hd->win, theGC, hd->x, hd->y, x,y);
  2110.     
  2111.       if (flags & HD_CLHNDS) 
  2112.     XFillRectangle(theDisp, hd->win, theGC, x-2,y-2, 5,5);
  2113.       else {
  2114.     XSetForeground(theDisp, theGC, hd->bg);
  2115.     XFillRectangle(theDisp, hd->win, theGC, x-1, y-1, 3,3);
  2116.     XSetForeground(theDisp, theGC, hd->fg);
  2117.     XDrawPoint(theDisp, hd->win, theGC, x, y);
  2118.     XDrawRectangle(theDisp, hd->win, theGC, x-2, y-2, 4,4);
  2119.       }
  2120.     }
  2121.  
  2122.     else {  /* not a range;  hue/sat dial */
  2123.       int r;
  2124.  
  2125.       /* compute x,y position from stval/satval */
  2126.       a = hdg2xdg(hd->stval) * DEG2RAD;
  2127.       r = ((HD_RADIUS - 4) * hd->satval) / 100;
  2128.       pol2xy(hd->x, hd->y, a, r, &x, &y);
  2129.  
  2130.       if (flags & HD_CLHNDS) 
  2131.     XFillRectangle(theDisp, hd->win, theGC, x-2,y-2, 5,5);
  2132.       else {
  2133.     XFillRectangle(theDisp, hd->win, theGC, hd->x-1, hd->y-1, 3,3);
  2134.  
  2135.     XSetForeground(theDisp, theGC, hd->bg);
  2136.     XFillRectangle(theDisp, hd->win, theGC, x-1, y-1, 3,3);
  2137.     XSetForeground(theDisp, theGC, hd->fg);
  2138.     XDrawPoint(theDisp, hd->win, theGC, x, y);
  2139.     XDrawRectangle(theDisp, hd->win, theGC, x-2, y-2, 4,4);
  2140.       }
  2141.     }
  2142.   }
  2143.     
  2144.  
  2145.  
  2146.  
  2147.   if ((flags & HD_DIR || flags & HD_CLHNDS) && hd->range) {
  2148.     int xdg1, xdg2, xdlen;
  2149.  
  2150.     if (flags & HD_CLHNDS) XSetForeground(theDisp, theGC, hd->bg);
  2151.                       else XSetForeground(theDisp, theGC, hd->fg);
  2152.  
  2153.     if (hd->ccwise) {
  2154.       xdg1 = hdg2xdg(hd->stval);
  2155.       xdg2 = hdg2xdg(hd->enval);
  2156.     }
  2157.     else {
  2158.       xdg1 = hdg2xdg(hd->enval);
  2159.       xdg2 = hdg2xdg(hd->stval);
  2160.     }
  2161.  
  2162.     xdlen = xdg2 - xdg1;
  2163.     if (xdlen<0) xdlen += 360;   /* note: 0 len means no range */
  2164.  
  2165.     if (xdlen>1) {
  2166.       XDrawArc(theDisp, hd->win, theGC, hd->x - ((HD_RADIUS*3)/5),
  2167.            hd->y - ((HD_RADIUS*3)/5), (HD_RADIUS*6)/5, (HD_RADIUS*6)/5,
  2168.            xdg1 * 64, xdlen * 64);
  2169.     }
  2170.  
  2171.     if (xdlen > 16) {
  2172.       xdg1 = hdg2xdg(hd->enval);
  2173.       if (hd->ccwise) xdg2 = xdg1-10;
  2174.                  else xdg2 = xdg1+10;
  2175.  
  2176.       pol2xy(hd->x, hd->y, xdg2 * DEG2RAD, ((HD_RADIUS*3)/5) + 3, &x, &y);
  2177.       pol2xy(hd->x, hd->y, xdg1 * DEG2RAD, ((HD_RADIUS*3)/5),     &x1,&y1);
  2178.       XDrawLine(theDisp, hd->win, theGC, x, y, x1, y1);
  2179.  
  2180.       if (hd->ccwise) xdg2 = xdg1-16;
  2181.                  else xdg2 = xdg1+16;
  2182.       pol2xy(hd->x, hd->y, xdg2 * DEG2RAD, ((HD_RADIUS*3)/5) - 2, &x, &y);
  2183.       XDrawLine(theDisp, hd->win, theGC, x, y, x1, y1);
  2184.     }
  2185.   }
  2186.  
  2187.  
  2188.   if (flags & HD_VALS) {
  2189.     char vstr[32];
  2190.  
  2191.     XSetFont(theDisp, theGC, monofont);
  2192.     XSetForeground(theDisp, theGC, hd->fg);
  2193.     XSetBackground(theDisp, theGC, hd->bg);
  2194.  
  2195.     if (hd->range) {
  2196.       sprintf(vstr,"%3d\007,%3d\007 %s", hd->stval, hd->enval, 
  2197.           hd->ccwise ? "CCW" : " CW");
  2198.     }
  2199.     else {
  2200.       sprintf(vstr,"%3d\007 %3d%%", hd->stval, hd->satval);
  2201.     }
  2202.       
  2203.     XDrawImageString(theDisp, hd->win, theGC,
  2204.              hd->x - XTextWidth(monofinfo, vstr, strlen(vstr))/2,
  2205.              hd->y + HD_RADIUS + 24, vstr, strlen(vstr));
  2206.     XSetFont(theDisp, theGC, mfont);
  2207.   }
  2208.  
  2209.  
  2210.   if (flags & HD_TITLE) {
  2211.     XSetForeground(theDisp, theGC, hd->fg);
  2212.     ULineString(hd->win, hd->str, hd->x - HD_RADIUS - 15,
  2213.         hd->y - HD_RADIUS - 4);
  2214.   }
  2215.  
  2216.  
  2217.   if (flags & HD_BUTTS) {
  2218.     if (hd->range) {
  2219.       for (i=0; i<N_HDBUTT; i++) BTRedraw(&hd->hdbutt[i]);
  2220.     }
  2221.     else {
  2222.       for (i=0; i<N_HDBUTT2; i++) BTRedraw(&hd->hdbutt[i]);
  2223.       CBRedraw(&hd->enabCB);
  2224.     }
  2225.   }
  2226.  
  2227.   if (!hd->range && !hd->enabCB.val) {  /* draw dimmed */
  2228.     XSync(theDisp, False);
  2229.     DimRect(hd->win, hd->x-HD_RADIUS-15, hd->y-HD_RADIUS-4-ASCENT,
  2230.         2*HD_RADIUS+30,(2*HD_RADIUS+4+ASCENT+80), hd->bg);
  2231.     XSync(theDisp, False);
  2232.     CBRedraw(&hd->enabCB);
  2233.   }
  2234. }
  2235.  
  2236.  
  2237.     
  2238. /**************************************************/
  2239. static int HDClick(hd,mx,my)
  2240. HDIAL *hd;
  2241. int mx, my;
  2242. {
  2243.   /* called when a click received.  checks whether HDTrack needs to be
  2244.      called.  Returns '1' if click is in HD dial area, 0 otherwise */
  2245.  
  2246.   int bnum,maxb;
  2247.   BUTT *bp;
  2248.  
  2249.   if (CBClick(&hd->enabCB, mx, my)) {
  2250.     if (CBTrack(&hd->enabCB)) {
  2251.       HDRedraw(hd, HD_ALL);
  2252.       return 1;
  2253.     }
  2254.   }
  2255.  
  2256.   if (!hd->range && !hd->enabCB.val) return 0;    /* disabled */
  2257.  
  2258.  
  2259.   if ( ((mx - hd->x) * (mx - hd->x)  +  (my - hd->y) * (my - hd->y)) 
  2260.       < (HD_RADIUS * HD_RADIUS)) {
  2261.     return HDTrack(hd,mx,my);
  2262.   }
  2263.  
  2264.   if (hd->range) maxb = N_HDBUTT;  else maxb = N_HDBUTT2;
  2265.  
  2266.   for (bnum=0; bnum<maxb; bnum++) {
  2267.     bp = &hd->hdbutt[bnum];
  2268.     if (PTINRECT(mx,my, bp->x, bp->y, bp->w, bp->h)) break;
  2269.   }
  2270.   if (bnum==maxb) return 0;
  2271.  
  2272.   if (bnum==HDB_FLIP && hd->range) {
  2273.     if (BTTrack(bp)) {
  2274.       int t;
  2275.       HDRedraw(hd, HD_CLHNDS);
  2276.       t = hd->stval;  hd->stval = hd->enval;  hd->enval = t;
  2277.       hd->ccwise = !hd->ccwise;
  2278.       HDRedraw(hd, HD_HANDS | HD_DIR | HD_VALS);
  2279.       return 1;
  2280.     }
  2281.     return 0;
  2282.   }
  2283.  
  2284.   else {    /* track buttons til mouse up */
  2285.     Window rW, cW;
  2286.     int    rx,ry,x,y;
  2287.     unsigned int mask;
  2288.  
  2289.     bp->lit = 1;  BTRedraw(bp);  /* light it up */
  2290.  
  2291.     /* loop until mouse is released */
  2292.     while (XQueryPointer(theDisp,hd->win,&rW,&cW,&rx,&ry,&x,&y,&mask)) {
  2293.       if (!(mask & Button1Mask)) break;    /* button released */
  2294.  
  2295.       /* check to see if state needs toggling */
  2296.       if (( bp->lit && !PTINRECT(x,y,bp->x,bp->y,bp->w,bp->h)) ||
  2297.       (!bp->lit &&  PTINRECT(x,y,bp->x,bp->y,bp->w,bp->h))) {
  2298.     bp->lit = !bp->lit;
  2299.     BTRedraw(bp);
  2300.       }
  2301.  
  2302.       if (bp->lit) {
  2303.     switch (bnum) {
  2304.     case HDB_ROTL:
  2305.       HDRedraw(hd, HD_CLHNDS);
  2306.       hd->stval--;  if (hd->stval<0) hd->stval += 360;
  2307.       hd->enval--;  if (hd->enval<0) hd->enval += 360;
  2308.       HDRedraw(hd, HD_HANDS | HD_DIR | HD_VALS);
  2309.       break;
  2310.  
  2311.     case HDB_ROTR:
  2312.       HDRedraw(hd, HD_CLHNDS);
  2313.       hd->stval++;  if (hd->stval>=360) hd->stval -= 360;
  2314.       hd->enval++;  if (hd->enval>=360) hd->enval -= 360;
  2315.       HDRedraw(hd, HD_HANDS | HD_DIR | HD_VALS);
  2316.       break;
  2317.  
  2318.     /* case HDB_DESAT: */
  2319.     /* case HDB_SAT:   */
  2320.     case HDB_EXPND:
  2321.     case HDB_SHRNK:
  2322.       if (hd->range) {
  2323.         if ((bnum == HDB_EXPND &&
  2324.          ( hd->ccwise && hd->enval == (hd->stval+1)) ||
  2325.          (!hd->ccwise && hd->enval == (hd->stval-1)))    ||
  2326.         (bnum == HDB_SHRNK && hd->stval == hd->enval)) {
  2327.           /* can't get any bigger/smaller */
  2328.         }
  2329.  
  2330.         else {
  2331.           HDRedraw(hd, HD_CLHNDS);
  2332.           if ((bnum==HDB_EXPND &&  hd->ccwise) ||
  2333.           (bnum==HDB_SHRNK && !hd->ccwise)) {
  2334.         if ((hd->stval & 1) == (hd->enval & 1))
  2335.           hd->stval = (hd->stval + 1 + 360) % 360;
  2336.         else
  2337.           hd->enval = (hd->enval - 1 + 360) % 360;
  2338.           }
  2339.  
  2340.           else {
  2341.         if ((hd->stval & 1) == (hd->enval & 1))
  2342.           hd->stval = (hd->stval - 1 + 360) % 360;
  2343.         else
  2344.           hd->enval = (hd->enval + 1 + 360) % 360;
  2345.           }
  2346.           HDRedraw(hd, HD_HANDS | HD_DIR | HD_VALS);
  2347.         }
  2348.       }
  2349.  
  2350.  
  2351.       else {   /* hue/sat dial:  SAT/DESAT */
  2352.         if (bnum == HDB_DESAT && hd->satval>0) {
  2353.           HDRedraw(hd, HD_CLHNDS);
  2354.           hd->satval--;  if (hd->satval<0) hd->satval = 0;
  2355.           HDRedraw(hd, HD_HANDS | HD_VALS);
  2356.         }
  2357.           
  2358.         else if (bnum == HDB_SAT && hd->satval<100) {
  2359.           HDRedraw(hd, HD_CLHNDS);
  2360.           hd->satval++;  if (hd->satval>100) hd->satval = 100;
  2361.           HDRedraw(hd, HD_HANDS | HD_VALS);
  2362.         }
  2363.       }
  2364.           
  2365.       break;
  2366.       
  2367.     }
  2368.     Timer(150);
  2369.       }
  2370.     }
  2371.  
  2372.     XFlush(theDisp);
  2373.   }
  2374.  
  2375.   if (bp->lit) {  bp->lit = 0;  BTRedraw(bp); }
  2376.     
  2377.   return 1;
  2378. }
  2379.  
  2380.  
  2381.  
  2382. /**************************************************/
  2383. static int HDTrack(hd,mx,my)
  2384. HDIAL *hd;
  2385. int mx,my;
  2386. {
  2387.   /* called when clicked in dial area.  tracks dragging handles around...
  2388.      returns '1' if anything changed */
  2389.  
  2390.   Window       rW,cW;
  2391.   int          rx,ry, x,y, ival,j, rv;
  2392.   unsigned int mask;
  2393.  
  2394.   rv = 0;
  2395.  
  2396.   if (!hd->range) {     /* hue/sat dial */
  2397.     int ihue, isat, newhue, newsat;
  2398.     double dx,dy,dist;
  2399.  
  2400.     ihue = hd->stval;  isat = hd->satval;
  2401.  
  2402.     /* loop until mouse is released */
  2403.     while (XQueryPointer(theDisp,hd->win,&rW,&cW,&rx,&ry,&x,&y,&mask)) {
  2404.       if (!(mask & Button1Mask)) break;    /* button released */
  2405.  
  2406.       /* compute new value based on mouse pos */
  2407.       newhue = computeHDval(hd, x,y);
  2408.       if (newhue<0) newhue = hd->stval;  /* at 'spaz' point, keep hue const */
  2409.  
  2410.       dx = x - hd->x;  dy = y - hd->y;
  2411.       dist = sqrt(dx*dx + dy*dy);
  2412.       
  2413.       newsat = (int) (dist / ((double) (HD_RADIUS - 4)) * 100);
  2414.       RANGE(newsat,0,100);
  2415.  
  2416.       if (newhue != hd->stval || newsat != hd->satval) {
  2417.     HDRedraw(hd, HD_CLHNDS);
  2418.     hd->stval = newhue;  hd->satval = newsat;
  2419.     HDRedraw(hd, HD_HANDS | HD_VALS);
  2420.       }
  2421.     }
  2422.  
  2423.     rv = (hd->stval != ihue || hd->satval != isat);
  2424.   }
  2425.  
  2426.   else {   /* the hard case */
  2427.     double a;
  2428.     int    handle = 0, *valp;
  2429.  
  2430.     /* determine if we're in either of the handles */
  2431.     a = hdg2xdg(hd->stval) * DEG2RAD;
  2432.     pol2xy(hd->x, hd->y, a, HD_RADIUS-4, &x,&y);
  2433.     if (PTINRECT(mx,my,x-3,y-3,7,7)) handle = 1;
  2434.  
  2435.     a = hdg2xdg(hd->enval) * DEG2RAD;
  2436.     pol2xy(hd->x, hd->y, a, HD_RADIUS-4, &x,&y);
  2437.     if (PTINRECT(mx,my,x-3,y-3,7,7)) handle = 2;
  2438.     
  2439.  
  2440.  
  2441.     if (!handle) {  /* not in either, rotate both */
  2442.       int oldj, len, origj;
  2443.  
  2444.       if (!hd->ccwise) {
  2445.     len = ((hd->enval - hd->stval + 360) % 360);
  2446.     oldj = (hd->stval + len/2 + 360) % 360;
  2447.       }
  2448.       else {
  2449.     len = ((hd->stval - hd->enval + 360) % 360);
  2450.     oldj = (hd->enval + len/2 + 360) % 360;
  2451.       }
  2452.  
  2453.       origj = j = oldj;
  2454.  
  2455.       while (XQueryPointer(theDisp,hd->win,&rW,&cW,&rx,&ry,&x,&y,&mask)) {
  2456.     if (!(mask & Button1Mask)) break;    /* button released */
  2457.  
  2458.     /* compute new value based on mouse pos */
  2459.     j = computeHDval(hd, x,y);
  2460.     if (j>=0 && j != oldj) {
  2461.       oldj = j;
  2462.       HDRedraw(hd, HD_CLHNDS);
  2463.       if (!hd->ccwise) {
  2464.         hd->stval = (j - len/2 + 360) % 360;
  2465.         hd->enval = (j + len/2 + 360) % 360;
  2466.       }
  2467.       else {
  2468.         hd->stval = (j + len/2 + 360) % 360;
  2469.         hd->enval = (j - len/2 + 360) % 360;
  2470.       }
  2471.       HDRedraw(hd, HD_HANDS | HD_DIR | HD_VALS);
  2472.     }
  2473.       }
  2474.       rv = (origj != j);
  2475.     }
  2476.         
  2477.  
  2478.     else {  /* in one of the handles */
  2479.       if (handle==1) valp = &(hd->stval);  else valp = &(hd->enval);
  2480.       ival = *valp;
  2481.  
  2482.       /* loop until mouse is released */
  2483.       while (XQueryPointer(theDisp,hd->win,&rW,&cW,&rx,&ry,&x,&y,&mask)) {
  2484.     if (!(mask & Button1Mask)) break;    /* button released */
  2485.  
  2486.     /* compute new value based on mouse pos */
  2487.     j = computeHDval(hd, x,y);
  2488.     if (j>=0 && j != *valp) {
  2489.       int ndist, ddist;
  2490.  
  2491.       HDRedraw(hd, HD_CLHNDS);
  2492.  
  2493.       if (!hd->ccwise) {
  2494.         ddist = (hd->enval - hd->stval + 360) % 360;
  2495.         if (handle==1) 
  2496.           ndist = (hd->enval - j + 360) % 360;
  2497.         else
  2498.           ndist = (j - hd->stval + 360) % 360;
  2499.       }
  2500.       else {
  2501.         ddist = (hd->stval - hd->enval + 360) % 360;
  2502.         if (handle==1) 
  2503.           ndist = (j - hd->enval + 360) % 360;
  2504.         else
  2505.           ndist = (hd->stval - j + 360) % 360;
  2506.       }
  2507.  
  2508.       if (abs(ddist - ndist) >= 180 && ddist<180) 
  2509.         hd->ccwise = !hd->ccwise;
  2510.       
  2511.       *valp = j;
  2512.       HDRedraw(hd, HD_HANDS | HD_DIR | HD_VALS);
  2513.     }
  2514.       }
  2515.       rv = (*valp != ival);
  2516.     }
  2517.   }
  2518.  
  2519.   return rv;
  2520. }
  2521.     
  2522.       
  2523.  
  2524. /**************************************************/
  2525. static int hdg2xdg(hdg)
  2526. int hdg;
  2527. {
  2528.   int xdg;
  2529.  
  2530.   xdg = 270 - hdg;
  2531.   if (xdg < 0)   xdg += 360;
  2532.   if (xdg > 360) xdg -= 360;
  2533.  
  2534.   return xdg;
  2535. }
  2536.  
  2537.  
  2538. /**************************************************/
  2539. static void pol2xy(cx, cy, ang, rad, xp, yp)
  2540. int cx, cy, rad, *xp, *yp;
  2541. double ang;
  2542. {
  2543.   *xp = cx + (int) (cos(ang) * (double) rad);
  2544.   *yp = cy - (int) (sin(ang) * (double) rad);
  2545. }
  2546.  
  2547.   
  2548. /***************************************************/
  2549. static int computeHDval(hd, x, y)
  2550. HDIAL *hd;
  2551. int x, y;
  2552. {
  2553.   int dx, dy;
  2554.   double angle;
  2555.  
  2556.   /* compute dx, dy (distance from center).  Note: +dy is *up* */
  2557.   dx = x - hd->x;  dy = hd->y - y;
  2558.  
  2559.   /* if too close to center, return -1 (invalid) avoid 'spazzing' */
  2560.   if (abs(dx) < 3 && abs(dy) < 3) return -1;
  2561.  
  2562.   /* figure out angle of vector dx,dy */
  2563.   if (dx==0) {     /* special case */
  2564.     if (dy>0) angle =  90.0;
  2565.          else angle = -90.0;
  2566.   }
  2567.   else if (dx>0) angle = atan((double)  dy / (double)  dx) * RAD2DEG;
  2568.   else           angle = atan((double) -dy / (double) -dx) * RAD2DEG + 180.0;
  2569.  
  2570.   angle = 270 - angle;   /* map into h-degrees */
  2571.   if (angle >= 360.0) angle -= 360.0;
  2572.   if (angle <    0.0) angle += 360.0;
  2573.  
  2574.   return (int) angle;
  2575. }
  2576.  
  2577.  
  2578.  
  2579.     
  2580. /****************************************************/
  2581. static void initHmap()
  2582. {
  2583.   int i;
  2584.   for (i=0; i<N_HMAP; i++) init1hmap(i);
  2585. }
  2586.  
  2587.  
  2588. /****************************************************/
  2589. static void init1hmap(i)
  2590. int i;
  2591. {
  2592.   int cang, width;
  2593.  
  2594.   width = 360 / N_HMAP;
  2595.  
  2596.   cang = i * width;
  2597.   hmap[i].src_st  = hmap[i].dst_st = (cang - width/2 + 360) % 360;
  2598.   hmap[i].src_en  = hmap[i].dst_en = (cang - width/2 + width +360) % 360;
  2599.   hmap[i].src_ccw = hmap[i].dst_ccw = 0;
  2600. }
  2601.  
  2602.  
  2603. /****************************************************/
  2604. static void dials2hmap()
  2605. {
  2606.   int i;
  2607.   i = RBWhich(hueRB);
  2608.  
  2609.   hmap[i].src_st  = srcHD.stval;
  2610.   hmap[i].src_en  = srcHD.enval;
  2611.   hmap[i].src_ccw = srcHD.ccwise;
  2612.  
  2613.   hmap[i].dst_st  = dstHD.stval;
  2614.   hmap[i].dst_en  = dstHD.enval;
  2615.   hmap[i].dst_ccw = dstHD.ccwise;
  2616. }
  2617.  
  2618.  
  2619. /****************************************************/
  2620. static void hmap2dials()
  2621. {
  2622.   int i;
  2623.   i = RBWhich(hueRB);
  2624.  
  2625.   srcHD.stval  = hmap[i].src_st;
  2626.   srcHD.enval  = hmap[i].src_en;
  2627.   srcHD.ccwise = hmap[i].src_ccw;
  2628.  
  2629.   dstHD.stval  = hmap[i].dst_st;
  2630.   dstHD.enval  = hmap[i].dst_en;
  2631.   dstHD.ccwise = hmap[i].dst_ccw;
  2632.  
  2633.   HDRedraw(&srcHD, HD_ALL | HD_CLEAR);
  2634.   HDRedraw(&dstHD, HD_ALL | HD_CLEAR);
  2635. }
  2636.  
  2637.  
  2638. /****************************************************/
  2639. static void build_hremap()
  2640. {
  2641.   int i,j, st1, en1, inc1, len1, st2, en2, inc2, len2;
  2642.   int a1, a2;
  2643.  
  2644.   /* start with a 1:1 mapping */
  2645.   for (i=0; i<360; i++) hremap[i] = i;
  2646.  
  2647.   for (i=0; i<N_HMAP; i++) {
  2648.     if ((hmap[i].src_st  != hmap[i].dst_st) ||
  2649.     (hmap[i].src_en  != hmap[i].dst_en) ||
  2650.     (hmap[i].src_ccw != hmap[i].dst_ccw)) {   /* not a 1:1 mapping */
  2651.  
  2652.       st1  = hmap[i].src_st;  
  2653.       en1  = hmap[i].src_en;
  2654.       if (hmap[i].src_ccw) {
  2655.     inc1 = -1; 
  2656.     len1 = (st1 - en1 + 360) % 360;
  2657.       }
  2658.       else {
  2659.     inc1 = 1;
  2660.     len1 = (en1 - st1 + 360) % 360;
  2661.       }
  2662.  
  2663.       st2 = hmap[i].dst_st;
  2664.       en2 = hmap[i].dst_en;
  2665.       if (hmap[i].dst_ccw) {
  2666.     inc2 = -1; 
  2667.     len2 = (st2 - en2 + 360) % 360;
  2668.       }
  2669.       else {
  2670.     inc2 = 1;
  2671.     len2 = (en2 - st2 + 360) % 360;
  2672.       }
  2673.  
  2674.       if (len1==0) {
  2675.     a1 = st1;
  2676.     a2 = st2;
  2677.     hremap[a1] = a2;
  2678.       }
  2679.       else {
  2680.     for (j=0, a1=st1; j<=len1; a1 = (a1 + inc1 + 360) % 360, j++) {
  2681.       a2 = (((inc2 * len2 * j) / len1 + st2) + 360) % 360;
  2682.       hremap[a1] = a2;
  2683.     }
  2684.       }
  2685.     }
  2686.   }
  2687. }
  2688.  
  2689.